<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>


</head>

<body>
<div class="article-body">

    <div class="article-intro">

        <h1>Python3 实例</h1>
        <p>以下实例在 Python3.4.3 版本下测试通过：</p>
        <ul>
            <li><a href="/python3/python3-helloworld.html" title="python Hello World 实例"> Python Hello World 实例 </a>
            </li>
            <li><a href="python3-add-number.html" title="python Hello World 实例"> Python 数字求和 </a></li>
            <li><a href="python3-square-root.html" title="python Hello World 实例"> Python 平方根 </a></li>
            <li><a href="python3-quadratic-roots.html" target="_blank">Python 二次方程</a></li>
            <li><a href="python3-area-triangle.html" target="_blank">Python 计算三角形的面积</a></li>
            <li><a href="python3-random-number.html" target="_blank">Python 随机数生成</a></li>
            <li><a href="python3-celsius-fahrenheit.html" target="_blank">Python 摄氏温度转华氏温度</a></li>

            <li><a href="python3-swap-variables.html" target="_blank">Python 交换变量</a></li>
            <li><a href="python3-if-example.html" target="_blank">Python if 语句</a></li>
            <li><a href="python3-check-is-number.html" target="_blank">Python 判断字符串是否为数字</a></li>
            <li><a href="python3-odd-even.html" target="_blank">Python 判断奇数偶数</a></li>
            <li><a href="python3-leap-year.html" target="_blank">Python 判断闰年</a></li>
            <li><a href="python3-largest-number.html" target="_blank">Python 获取最大值函数</a></li>
            <li><a href="python3-prime-number.html" target="_blank">Python 质数判断</a></li>
            <li><a href="python3-prime-number-intervals.html" target="_blank">Python 输出指定范围内的素数</a></li>
            <li><a href="python3-factorial.html" target="_blank">Python 阶乘实例</a></li>
            <li><a href="python3-99-table.html" target="_blank">Python 九九乘法表</a></li>
            <li><a href="python3-fibonacci-sequence.html" target="_blank">Python 斐波那契数列</a></li>
            <li><a href="python3-armstrong-number.html" target="_blank">Python 阿姆斯特朗数</a></li>
            <li><a href="python3-conversion-binary-octal-hexadecimal.html" target="_blank">Python 十进制转二进制、八进制、十六进制</a>
            </li>
            <li><a href="python3-ascii-character.html" target="_blank">Python ASCII码与字符相互转换</a></li>
            <li><a href="python3-hcf.html" target="_blank">Python 最大公约数算法</a></li>
            <li><a href="python3-lcm.html" target="_blank">Python 最小公倍数算法</a></li>
            <li><a href="python3-calculator.html" target="_blank">Python 简单计算器实现</a></li>
            <li><a href="python3-calendar.html" target="_blank">Python 生成日历</a></li>
            <li><a href="python3-fibonacci-recursion.html" target="_blank">Python 使用递归斐波那契数列</a></li>
            <li><a href="python3-file-io.html" target="_blank">Python 文件 IO</a></li>
            <li><a href="python3-check-string.html" target="_blank">Python 字符串判断</a></li>
            <li><a href="python3-upper-lower.html" target="_blank">Python 字符串大小写转换</a></li>
            <li><a href="python3-month-days.html" target="_blank">Python 计算每个月天数</a></li>
            <li><a href="python3-get-yesterday.html" target="_blank">Python 获取昨天日期</a></li>
            <li><a href="python3-list-operator.html" target="_blank">Python list 常用操作</a></li>
        </ul>

    </div>
</div>

<div class="article-body">

    <div class="article-intro">

        <h1>Python Hello World 实例</h1>


        <p>以下实例为学习Python的第一个实例，即如何输出"Hello World!"：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com"># -*- coding: UTF-8 -*-</span><span
                class="pln">

</span><span class="com"># Filename : helloworld.py</span><span class="pln">
</span><span class="com"># author by : www.runoob.com</span><span class="pln">

</span><span class="com"># 该实例输出 Hello World!</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="str">'Hello World!'</span><span class="pun">)</span></pre>
        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">Hello</span><span class="pln"> </span><span
                class="typ">World</span><span class="pun">！</span></pre>


    </div>
</div>
<div class="article-body">

    <div class="article-intro">

        <h1>Python 数字求和</h1>


        <p>以下实例为通过用户输入两个数字，并计算两个数字之和：</p>
        <div class="example">
            <div class="example">实例(Python 3.0+)</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment"># -*- coding: UTF-8 -*-</span><span class="hl-code">

</span><span class="hl-comment"># Filename : test.py</span><span class="hl-code">
</span><span class="hl-comment"># author by : www.runoob.com</span><span class="hl-code">

</span><span class="hl-comment"># 用户输入数字</span><span class="hl-code">
</span><span class="hl-identifier">num1</span><span class="hl-code"> = </span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">输入第一个数字：</span><span class="hl-quotes">'</span><span
                        class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">num2</span><span class="hl-code"> = </span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">输入第二个数字：</span><span class="hl-quotes">'</span><span
                        class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-comment"># 求和</span><span class="hl-code">
</span><span class="hl-identifier">sum</span><span class="hl-code"> = </span><span class="hl-builtin">float</span><span
                        class="hl-brackets">(</span><span class="hl-identifier">num1</span><span
                        class="hl-brackets">)</span><span class="hl-code"> + </span><span
                        class="hl-builtin">float</span><span class="hl-brackets">(</span><span class="hl-identifier">num2</span><span
                        class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-comment"># 显示计算结果</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">数字 {0} 和 {1} 相加结果为： {2}</span><span class="hl-quotes">'</span><span
                        class="hl-code">.</span><span class="hl-identifier">format</span><span
                        class="hl-brackets">(</span><span class="hl-identifier">num1</span><span
                        class="hl-code">, </span><span class="hl-identifier">num2</span><span
                        class="hl-code">, </span><span class="hl-identifier">sum</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span></pre>
            </div>
        </div>
        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">输入第一个数字：</span><span
                class="lit">1.5</span><span class="pln">
</span><span class="pun">输入第二个数字：</span><span class="lit">2.5</span><span class="pln">
</span><span class="pun">数字</span><span class="pln"> </span><span class="lit">1.5</span><span class="pln"> </span><span
                class="pun">和</span><span class="pln"> </span><span class="lit">2.5</span><span
                class="pln"> </span><span class="pun">相加结果为：</span><span class="pln"> </span><span
                class="lit">4.0</span></pre>
        <p>
            在该实例中，我们通过用户输入两个数字来求和。使用了内置函数 input() 来获取用户的输入，input() 返回一个字符串，所以我们需要使用 float() 方法将字符串转换为数字。</p>
        <p>两数字运算，求和我们使用了加号 (+)运算符，除此外，还有 减号 (-), 乘号 (*), 除号 (/), 地板除 (//) 或 取余 (%)。更多数字运算可以查看我们的<a
                href="http://www.w3cschool.cc/python3/python3-number.html">Python 数字运算</a>。
        </p>
        <p>我们还可以将以上运算，合并为一行代码：</p>
        <p></p>
        <div class="example">
            <div class="example">实例(Python 3.0+)</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment"># -*- coding: UTF-8 -*-</span><span class="hl-code">

</span><span class="hl-comment"># Filename : test.py</span><span class="hl-code">
</span><span class="hl-comment"># author by : www.runoob.com</span><span class="hl-code">

</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">两数之和为 %.1f</span><span class="hl-quotes">'</span><span
                        class="hl-code"> %</span><span class="hl-brackets">(</span><span class="hl-builtin">float</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">输入第一个数字：</span><span class="hl-quotes">'</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span
                        class="hl-code">+</span><span class="hl-builtin">float</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">输入第二个数字：</span><span class="hl-quotes">'</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span
                        class="hl-brackets">)</span></pre>
            </div>
        </div>
        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">$ python test</span><span class="pun">.</span><span
                class="pln">py
</span><span class="pun">输入第一个数字：</span><span class="lit">1.5</span><span class="pln">
</span><span class="pun">输入第二个数字：</span><span class="lit">2.5</span><span class="pln">
</span><span class="pun">两数之和为</span><span class="pln"> </span><span class="lit">4.0</span></pre>

    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python 平方根</h1>

        <p>平方根，又叫二次方根，表示为〔√￣〕，如：数学语言为：√￣16=4。语言描述为：根号下16=4。</p>
        <p>以下实例为通过用户输入一个数字，并计算这个数字的平方根：</p>
        <div class="example">
            <div class="example">实例(Python 3.0+)</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment"># -*- coding: UTF-8 -*-</span><span class="hl-code">

</span><span class="hl-comment"># Filename : test.py</span><span class="hl-code">
</span><span class="hl-comment"># author by : www.runoob.com</span><span class="hl-code">

</span><span class="hl-identifier">num</span><span class="hl-code"> = </span><span class="hl-builtin">float</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">请输入一个数字： </span><span class="hl-quotes">'</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">num_sqrt</span><span class="hl-code"> = </span><span class="hl-identifier">num</span><span
                        class="hl-code"> ** </span><span class="hl-number">0.5</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string"> %0.3f 的平方根为 %0.3f</span><span class="hl-quotes">'</span><span
                        class="hl-code">%</span><span class="hl-brackets">(</span><span class="hl-identifier">num</span><span
                        class="hl-code"> ,</span><span class="hl-identifier">num_sqrt</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span></pre>
            </div>
        </div>
        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">$ python test</span><span class="pun">.</span><span
                class="pln">py
</span><span class="pun">请输入一个数字：</span><span class="pln"> </span><span class="lit">4</span><span class="pln">
 </span><span class="lit">4.000</span><span class="pln"> </span><span class="pun">的平方根为</span><span class="pln"> </span><span
                class="lit">2.000</span></pre>
        <p>
            在该实例中，我们通过用户输入一个数字，并使用指数运算符 ** 来计算改数的平方根。
        </p>
        <p>
            该程序只适用于正数。负数和复数可以使用以下的方式：</p>

        <p></p>
        <div class="example">
            <div class="example">实例(Python 3.0+)</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment"># -*- coding: UTF-8 -*-</span><span class="hl-code">

</span><span class="hl-comment"># Filename : test.py</span><span class="hl-code">
</span><span class="hl-comment"># author by : www.runoob.com</span><span class="hl-code">

</span><span class="hl-comment"># 计算实数和复数平方根</span><span class="hl-code">
</span><span class="hl-comment"># 导入复数数学模块</span><span class="hl-code">

</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span
                        class="hl-identifier">cmath</span><span class="hl-code">

</span><span class="hl-identifier">num</span><span class="hl-code"> = </span><span class="hl-builtin">int</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">请输入一个数字: </span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">num_sqrt</span><span class="hl-code"> = </span><span
                        class="hl-identifier">cmath</span><span class="hl-code">.</span><span
                        class="hl-identifier">sqrt</span><span class="hl-brackets">(</span><span class="hl-identifier">num</span><span
                        class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">{0} 的平方根为 {1:0.3f}+{2:0.3f}j</span><span class="hl-quotes">'</span><span
                        class="hl-code">.</span><span class="hl-identifier">format</span><span
                        class="hl-brackets">(</span><span class="hl-identifier">num</span><span
                        class="hl-code"> ,</span><span class="hl-identifier">num_sqrt</span><span
                        class="hl-code">.</span><span class="hl-identifier">real</span><span
                        class="hl-code">,</span><span class="hl-identifier">num_sqrt</span><span
                        class="hl-code">.</span><span class="hl-identifier">imag</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span></pre>
            </div>
        </div>
        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">$ python test</span><span class="pun">.</span><span
                class="pln">py
</span><span class="pun">请输入一个数字:</span><span class="pln"> </span><span class="pun">-</span><span
                class="lit">8</span><span class="pln">
</span><span class="pun">-</span><span class="lit">8</span><span class="pln"> </span><span class="pun">的平方根为</span><span
                class="pln"> </span><span class="lit">0.000</span><span class="pun">+</span><span
                class="lit">2.828j</span></pre>
        <p>
            该实例中，我们使用了 cmath (complex math) 模块的 sqrt() 方法。
        </p>

    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python 二次方程</h1>


        <p>以下实例为通过用户输入数字，并计算二次方程：</p>
        <div class="example">
            <div class="example">实例(Python 3.0+)</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment"># Filename : test.py</span><span class="hl-code">
</span><span class="hl-comment"># author by : www.runoob.com</span><span class="hl-code">

</span><span class="hl-comment"># 二次方程式 ax**2 + bx + c = 0</span><span class="hl-code">
</span><span class="hl-comment"># a、b、c 用户提供</span><span class="hl-code">

</span><span class="hl-comment"># 导入 cmath(复杂数学运算) 模块</span><span class="hl-code">
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span
                        class="hl-identifier">cmath</span><span class="hl-code">

</span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-builtin">float</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">输入 a: </span><span class="hl-quotes">'</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span class="hl-builtin">float</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">输入 b: </span><span class="hl-quotes">'</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-builtin">float</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">输入 c: </span><span class="hl-quotes">'</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-comment"># 计算</span><span class="hl-code">
</span><span class="hl-identifier">d</span><span class="hl-code"> = </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">b</span><span class="hl-code">**</span><span
                        class="hl-number">2</span><span class="hl-brackets">)</span><span
                        class="hl-code"> - </span><span class="hl-brackets">(</span><span
                        class="hl-number">4</span><span class="hl-code">*</span><span
                        class="hl-identifier">a</span><span class="hl-code">*</span><span class="hl-identifier">c</span><span
                        class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-comment"># 两种求解方式</span><span class="hl-code">
</span><span class="hl-identifier">sol1</span><span class="hl-code"> = </span><span class="hl-brackets">(</span><span
                        class="hl-code">-</span><span class="hl-identifier">b</span><span class="hl-code">-</span><span
                        class="hl-identifier">cmath</span><span class="hl-code">.</span><span
                        class="hl-identifier">sqrt</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">d</span><span class="hl-brackets">)</span><span
                        class="hl-brackets">)</span><span class="hl-code">/</span><span
                        class="hl-brackets">(</span><span class="hl-number">2</span><span class="hl-code">*</span><span
                        class="hl-identifier">a</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">sol2</span><span class="hl-code"> = </span><span class="hl-brackets">(</span><span
                        class="hl-code">-</span><span class="hl-identifier">b</span><span class="hl-code">+</span><span
                        class="hl-identifier">cmath</span><span class="hl-code">.</span><span
                        class="hl-identifier">sqrt</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">d</span><span class="hl-brackets">)</span><span
                        class="hl-brackets">)</span><span class="hl-code">/</span><span
                        class="hl-brackets">(</span><span class="hl-number">2</span><span class="hl-code">*</span><span
                        class="hl-identifier">a</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">结果为 {0} 和 {1}</span><span class="hl-quotes">'</span><span
                        class="hl-code">.</span><span class="hl-identifier">format</span><span
                        class="hl-brackets">(</span><span class="hl-identifier">sol1</span><span
                        class="hl-code">,</span><span class="hl-identifier">sol2</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span></pre>
            </div>
        </div>

        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">$ python test</span><span class="pun">.</span><span
                class="pln">py
</span><span class="pun">输入</span><span class="pln"> a</span><span class="pun">:</span><span class="pln"> </span><span
                class="lit">1</span><span class="pln">
</span><span class="pun">输入</span><span class="pln"> b</span><span class="pun">:</span><span class="pln"> </span><span
                class="lit">5</span><span class="pln">
</span><span class="pun">输入</span><span class="pln"> c</span><span class="pun">:</span><span class="pln"> </span><span
                class="lit">6</span><span class="pln">
</span><span class="pun">结果为</span><span class="pln"> </span><span class="pun">(-</span><span class="lit">3</span><span
                class="pun">+</span><span class="lit">0j</span><span class="pun">)</span><span class="pln"> </span><span
                class="pun">和</span><span class="pln"> </span><span class="pun">(-</span><span class="lit">2</span><span
                class="pun">+</span><span class="lit">0j</span><span class="pun">)</span></pre>
        <p>
            该实例中，我们使用了 cmath (complex math) 模块的 sqrt() 方法 来计算平方根。
        </p>

    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python 计算三角形的面积</h1>


        <p>以下实例为通过用户输入三角形三边长度，并计算三角形的面积：</p>
        <div class="example">
            <div class="example">实例(Python 3.0+)</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment"># -*- coding: UTF-8 -*-</span><span class="hl-code">

</span><span class="hl-comment"># Filename : test.py</span><span class="hl-code">
</span><span class="hl-comment"># author by : www.runoob.com</span><span class="hl-code">


</span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-builtin">float</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">输入三角形第一边长: </span><span class="hl-quotes">'</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span class="hl-builtin">float</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">输入三角形第二边长: </span><span class="hl-quotes">'</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-builtin">float</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">输入三角形第三边长: </span><span class="hl-quotes">'</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-comment"># 计算半周长</span><span class="hl-code">
</span><span class="hl-identifier">s</span><span class="hl-code"> = </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">a</span><span class="hl-code"> + </span><span
                        class="hl-identifier">b</span><span class="hl-code"> + </span><span
                        class="hl-identifier">c</span><span class="hl-brackets">)</span><span class="hl-code"> / </span><span
                        class="hl-number">2</span><span class="hl-code">

</span><span class="hl-comment"># 计算面积</span><span class="hl-code">
</span><span class="hl-identifier">area</span><span class="hl-code"> = </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">s</span><span class="hl-code">*</span><span
                        class="hl-brackets">(</span><span class="hl-identifier">s</span><span
                        class="hl-code">-</span><span class="hl-identifier">a</span><span
                        class="hl-brackets">)</span><span class="hl-code">*</span><span
                        class="hl-brackets">(</span><span class="hl-identifier">s</span><span
                        class="hl-code">-</span><span class="hl-identifier">b</span><span
                        class="hl-brackets">)</span><span class="hl-code">*</span><span
                        class="hl-brackets">(</span><span class="hl-identifier">s</span><span
                        class="hl-code">-</span><span class="hl-identifier">c</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span
                        class="hl-code"> ** </span><span class="hl-number">0.5</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">三角形面积为 %0.2f</span><span class="hl-quotes">'</span><span
                        class="hl-code"> %</span><span class="hl-identifier">area</span><span
                        class="hl-brackets">)</span></pre>
            </div>
        </div>

        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">$ python test</span><span class="pun">.</span><span
                class="pln">py
</span><span class="pun">输入三角形第一边长:</span><span class="pln"> </span><span class="lit">5</span><span class="pln">
</span><span class="pun">输入三角形第二边长:</span><span class="pln"> </span><span class="lit">6</span><span class="pln">
</span><span class="pun">输入三角形第三边长:</span><span class="pln"> </span><span class="lit">7</span><span class="pln">
</span><span class="pun">三角形面积为</span><span class="pln"> </span><span class="lit">14.70</span></pre>


    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python 随机数生成</h1>


        <p>以下实例演示了如何生成一个随机数：</p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment"># -*- coding: UTF-8 -*-</span><span class="hl-code">

</span><span class="hl-comment"># Filename : test.py</span><span class="hl-code">
</span><span class="hl-comment"># author by : www.runoob.com</span><span class="hl-code">

</span><span class="hl-comment"># 生成 0 ~ 9 之间的随机数</span><span class="hl-code">

</span><span class="hl-comment"># 导入 random(随机数) 模块</span><span class="hl-code">
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span
                        class="hl-identifier">random</span><span class="hl-code">

</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">random</span><span class="hl-code">.</span><span class="hl-identifier">randint</span><span
                        class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">,</span><span
                        class="hl-number">9</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span>
                </pre>
            </div>
        </div>

        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="lit">4</span></pre>
        <p>
            以上实例我们使用了 random 模块的 randint() 函数来生成随机数，你每次执行后都返回不同的数字（0 到 9），该函数的语法为：
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">random</span><span class="pun">.</span><span
                class="pln">randint</span><span class="pun">(</span><span class="pln">a</span><span class="pun">,</span><span
                class="pln">b</span><span class="pun">)</span></pre>
        <p>函数返回数字 N ，N 为 a 到 b 之间的数字（a &lt;= N &lt;= b），包含 a 和 b。</p>

    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python 摄氏温度转华氏温度</h1>


        <p>以下实例演示了如何将摄氏温度转华氏温度：</p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment"># -*- coding: UTF-8 -*-</span><span class="hl-code">

</span><span class="hl-comment"># Filename : test.py</span><span class="hl-code">
</span><span class="hl-comment"># author by : www.runoob.com</span><span class="hl-code">

</span><span class="hl-comment"># 用户输入摄氏温度</span><span class="hl-code">

</span><span class="hl-comment"># 接收用户收入</span><span class="hl-code">
</span><span class="hl-identifier">celsius</span><span class="hl-code"> = </span><span
                        class="hl-builtin">float</span><span class="hl-brackets">(</span><span
                        class="hl-builtin">input</span><span class="hl-brackets">(</span><span
                        class="hl-quotes">'</span><span class="hl-string">输入摄氏温度: </span><span
                        class="hl-quotes">'</span><span class="hl-brackets">)</span><span
                        class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-comment"># 计算华氏温度</span><span class="hl-code">
</span><span class="hl-identifier">fahrenheit</span><span class="hl-code"> = </span><span
                        class="hl-brackets">(</span><span class="hl-identifier">celsius</span><span
                        class="hl-code"> * </span><span class="hl-number">1.8</span><span
                        class="hl-brackets">)</span><span class="hl-code"> + </span><span
                        class="hl-number">32</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">%0.1f 摄氏温度转为华氏温度为 %0.1f </span><span class="hl-quotes">'</span><span
                        class="hl-code"> %</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">celsius</span><span class="hl-code">,</span><span class="hl-identifier">fahrenheit</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span></pre>
            </div>
        </div>

        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">输入摄氏温度:</span><span class="pln"> </span><span
                class="lit">38</span><span class="pln">
</span><span class="lit">38.0</span><span class="pln"> </span><span class="pun">摄氏温度转为华氏温度为</span><span
                class="pln"> </span><span class="lit">100.4</span><span class="pln"> </span></pre>
        <p>
            以上实例中，摄氏温度转华氏温度的公式为 celsius * 1.8 = fahrenheit - 32。所以得到以下式子：
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">celsius </span><span class="pun">=</span><span
                class="pln"> </span><span class="pun">(</span><span class="pln">fahrenheit </span><span
                class="pun">-</span><span class="pln"> </span><span class="lit">32</span><span class="pun">)</span><span
                class="pln"> </span><span class="pun">/</span><span class="pln"> </span><span
                class="lit">1.8</span></pre>

    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python 交换变量</h1>


        <p>以下实例通过用户输入两个变量，并相互交换：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com"># -*- coding: UTF-8 -*-</span><span
                class="pln">

</span><span class="com"># Filename : test.py</span><span class="pln">
</span><span class="com"># author by : www.runoob.com</span><span class="pln">

</span><span class="com"># 用户输入</span><span class="pln">

x </span><span class="pun">=</span><span class="pln"> input</span><span class="pun">(</span><span
                class="str">'输入 x 值: '</span><span class="pun">)</span><span class="pln">
y </span><span class="pun">=</span><span class="pln"> input</span><span class="pun">(</span><span
                class="str">'输入 y 值: '</span><span class="pun">)</span><span class="pln">

</span><span class="com"># 创建临时变量，并交换</span><span class="pln">
temp </span><span class="pun">=</span><span class="pln"> x
x </span><span class="pun">=</span><span class="pln"> y
y </span><span class="pun">=</span><span class="pln"> temp

</span><span class="kwd">print</span><span class="pun">(</span><span class="str">'交换后 x 的值为: {}'</span><span
                class="pun">.</span><span class="pln">format</span><span class="pun">(</span><span
                class="pln">x</span><span class="pun">))</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="str">'交换后 y 的值为: {}'</span><span
                class="pun">.</span><span class="pln">format</span><span class="pun">(</span><span
                class="pln">y</span><span class="pun">))</span></pre>

        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">输入</span><span class="pln"> x </span><span
                class="pun">值:</span><span class="pln"> </span><span class="lit">2</span><span class="pln">
</span><span class="pun">输入</span><span class="pln"> y </span><span class="pun">值:</span><span class="pln"> </span><span
                class="lit">3</span><span class="pln">
</span><span class="pun">交换后</span><span class="pln"> x </span><span class="pun">的值为:</span><span
                class="pln"> </span><span class="lit">3</span><span class="pln">
</span><span class="pun">交换后</span><span class="pln"> y </span><span class="pun">的值为:</span><span
                class="pln"> </span><span class="lit">2</span></pre>
        <p>
            以上实例中，我们创建了临时变量 temp ，并将 x 的值存储在 temp 变量中，接着将 y 值赋给 x，最后将 temp 赋值给 y 变量。
        </p>
        <h3>不使用临时变量</h3>
        <p>我们也可以不创建临时变量，用一个非常优雅的方式来交换变量：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">x</span><span class="pun">,</span><span
                class="pln">y </span><span class="pun">=</span><span class="pln"> y</span><span
                class="pun">,</span><span class="pln">x</span></pre>
        <p>所以以上实例就可以修改为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com"># -*- coding: UTF-8 -*-</span><span
                class="pln">

</span><span class="com"># Filename : test.py</span><span class="pln">
</span><span class="com"># author by : www.runoob.com</span><span class="pln">

</span><span class="com"># 用户输入</span><span class="pln">

x </span><span class="pun">=</span><span class="pln"> input</span><span class="pun">(</span><span
                class="str">'输入 x 值: '</span><span class="pun">)</span><span class="pln">
y </span><span class="pun">=</span><span class="pln"> input</span><span class="pun">(</span><span
                class="str">'输入 y 值: '</span><span class="pun">)</span><span class="pln">

</span><span class="com"># 不使用临时变量</span><span class="pln">
x</span><span class="pun">,</span><span class="pln">y </span><span class="pun">=</span><span class="pln"> y</span><span
                class="pun">,</span><span class="pln">x

</span><span class="kwd">print</span><span class="pun">(</span><span class="str">'交换后 x 的值为: {}'</span><span
                class="pun">.</span><span class="pln">format</span><span class="pun">(</span><span
                class="pln">x</span><span class="pun">))</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="str">'交换后 y 的值为: {}'</span><span
                class="pun">.</span><span class="pln">format</span><span class="pun">(</span><span
                class="pln">y</span><span class="pun">))</span></pre>
        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">输入</span><span class="pln"> x </span><span
                class="pun">值:</span><span class="pln"> </span><span class="lit">1</span><span class="pln">
</span><span class="pun">输入</span><span class="pln"> y </span><span class="pun">值:</span><span class="pln"> </span><span
                class="lit">2</span><span class="pln">
</span><span class="pun">交换后</span><span class="pln"> x </span><span class="pun">的值为:</span><span
                class="pln"> </span><span class="lit">2</span><span class="pln">
</span><span class="pun">交换后</span><span class="pln"> y </span><span class="pun">的值为:</span><span
                class="pln"> </span><span class="lit">1</span></pre>

    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python if 语句</h1>


        <p>以下实例通过使用 <b>if...elif...else</b> 语句判断数字是正数、负数或零：</p>
        <pre class="prettyprint"># -*- coding: UTF-8 -*-

# Filename : test.py
# author by : www.runoob.com

# 用户输入数字

num = float(input("输入一个数字: "))
if num &gt; 0:
   print("正数")
elif num == 0:
   print("零")
else:
   print("负数")
</pre>
        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint">$ python test.py
输入一个数字: 3
正数
</pre>
        <p>我们也可以使用内嵌 if 语句来实现：</p>
        <pre class="prettyprint"># -*- coding: UTF-8 -*-

# Filename ：test.py
# author by : www.w3cschool.cc

# 内嵌 if 语句

num = float(input("输入一个数字: "))
if num &gt;= 0:
   if num == 0:
       print("零")
   else:
       print("正数")
else:
   print("负数")
</pre>
        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint">$ python test.py
输入一个数字: 0
零
</pre>

    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python 判断字符串是否为数字</h1>


        <p>以下实例通过创建自定义函数 <b>is_number()</b> 方法来判断字符串是否为数字：</p>
        <div class="example">
            <div class="example">实例(Python 3.0+)</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment"># -*- coding: UTF-8 -*-</span><span class="hl-code">

</span><span class="hl-comment"># Filename : test.py</span><span class="hl-code">
</span><span class="hl-comment"># author by : www.runoob.com</span><span class="hl-code">

</span><span class="hl-reserved">def</span><span class="hl-code"> </span><span
                        class="hl-identifier">is_number</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">s</span><span class="hl-brackets">)</span><span class="hl-code">:
    </span><span class="hl-reserved">try</span><span class="hl-code">:
        </span><span class="hl-builtin">float</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">s</span><span class="hl-brackets">)</span><span class="hl-code">
        </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span
                        class="hl-reserved">True</span><span class="hl-code">
    </span><span class="hl-reserved">except</span><span class="hl-code"> </span><span
                        class="hl-reserved">ValueError</span><span class="hl-code">:
        </span><span class="hl-reserved">pass</span><span class="hl-code">

    </span><span class="hl-reserved">try</span><span class="hl-code">:
        </span><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">unicodedata</span><span
                        class="hl-code">
        </span><span class="hl-identifier">unicodedata</span><span class="hl-code">.</span><span class="hl-identifier">numeric</span><span
                        class="hl-brackets">(</span><span class="hl-identifier">s</span><span
                        class="hl-brackets">)</span><span class="hl-code">
        </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span
                        class="hl-reserved">True</span><span class="hl-code">
    </span><span class="hl-identifier">except</span><span class="hl-code"> </span><span
                        class="hl-brackets">(</span><span class="hl-reserved">TypeError</span><span
                        class="hl-code">, </span><span class="hl-reserved">ValueError</span><span
                        class="hl-brackets">)</span><span class="hl-code">:
        </span><span class="hl-reserved">pass</span><span class="hl-code">

    </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span
                        class="hl-reserved">False</span><span class="hl-code">

</span><span class="hl-comment"># 测试字符串和数字</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">is_number</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">foo</span><span class="hl-quotes">'</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span
                        class="hl-code">   </span><span class="hl-comment"># False</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">is_number</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">1</span><span class="hl-quotes">'</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">     </span><span
                        class="hl-comment"># True</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">is_number</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">1.3</span><span class="hl-quotes">'</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span
                        class="hl-code">   </span><span class="hl-comment"># True</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">is_number</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">-1.37</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span
                        class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-comment"># True</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">is_number</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">1e3</span><span class="hl-quotes">'</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span
                        class="hl-code">   </span><span class="hl-comment"># True</span><span class="hl-code">

</span><span class="hl-comment"># 测试 Unicode</span><span class="hl-code">
</span><span class="hl-comment"># 阿拉伯语 5</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">is_number</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">٥</span><span class="hl-quotes">'</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span
                        class="hl-code">  </span><span class="hl-comment"># True</span><span class="hl-code">
</span><span class="hl-comment"># 泰语 2</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">is_number</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">๒</span><span class="hl-quotes">'</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span
                        class="hl-code">  </span><span class="hl-comment"># True</span><span class="hl-code">
</span><span class="hl-comment"># 中文数字</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">is_number</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">四</span><span class="hl-quotes">'</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span
                        class="hl-code"> </span><span class="hl-comment"># True</span><span class="hl-code">
</span><span class="hl-comment"># 版权号</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">is_number</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">©</span><span class="hl-quotes">'</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span
                        class="hl-code">  </span><span class="hl-comment"># False</span></pre>
            </div>
        </div>
        <p>我们也可以使用内嵌 if 语句来实现：</p>

        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="kwd">False</span><span class="pln">
</span><span class="kwd">True</span><span class="pln">
</span><span class="kwd">True</span><span class="pln">
</span><span class="kwd">True</span><span class="pln">
</span><span class="kwd">True</span><span class="pln">
</span><span class="kwd">True</span><span class="pln">
</span><span class="kwd">True</span><span class="pln">
</span><span class="kwd">True</span><span class="pln">
</span><span class="kwd">False</span></pre>
        <h3>更多方法</h3>
        <p>
            Python <a href="/python3/python3-string-isdigit.html">isdigit() </a>方法检测字符串是否只由数字组成。</p>
        <p>Python <a href="/python3/python3-string-isnumeric.html">isnumeric() </a>方法检测字符串是否只由数字组成。这种方法是只针对unicode对象。
        </p>

    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python 判断奇数偶数</h1>


        <p>以下实例用于判断一个数字是否为奇数或偶数：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com"># -*- coding: UTF-8 -*-</span><span
                class="pln">

</span><span class="com"># Filename : test.py</span><span class="pln">
</span><span class="com"># author by : www.runoob.com</span><span class="pln">

</span><span class="com"># Python 判断奇数偶数</span><span class="pln">
</span><span class="com"># 如果是偶数除于 2 余数为 0</span><span class="pln">
</span><span class="com"># 如果余数为 1 则为奇数</span><span class="pln">

num </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">int</span><span
                class="pun">(</span><span class="pln">input</span><span class="pun">(</span><span
                class="str">"输入一个数字: "</span><span class="pun">))</span><span class="pln">
</span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">num </span><span
                class="pun">%</span><span class="pln"> </span><span class="lit">2</span><span class="pun">)</span><span
                class="pln"> </span><span class="pun">==</span><span class="pln"> </span><span class="lit">0</span><span
                class="pun">:</span><span class="pln">
   </span><span class="kwd">print</span><span class="pun">(</span><span class="str">"{0} 是偶数"</span><span
                class="pun">.</span><span class="pln">format</span><span class="pun">(</span><span
                class="pln">num</span><span class="pun">))</span><span class="pln">
</span><span class="kwd">else</span><span class="pun">:</span><span class="pln">
   </span><span class="kwd">print</span><span class="pun">(</span><span class="str">"{0} 是奇数"</span><span
                class="pun">.</span><span class="pln">format</span><span class="pun">(</span><span
                class="pln">num</span><span class="pun">))</span></pre>
        <p>我们也可以使用内嵌 if 语句来实现：</p>

        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">输入一个数字:</span><span class="pln"> </span><span
                class="lit">3</span><span class="pln">
</span><span class="lit">3</span><span class="pln"> </span><span class="pun">是奇数</span></pre>


    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python 判断闰年</h1>


        <p>以下实例用于判断用户输入的年份是否为闰年：
        </p>
        <div class="example">
            <div class="example">实例(Python 3.0+)</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment"># -*- coding: UTF-8 -*-</span><span class="hl-code">

</span><span class="hl-comment"># Filename : test.py</span><span class="hl-code">
</span><span class="hl-comment"># author by : www.runoob.com</span><span class="hl-code">

</span><span class="hl-identifier">year</span><span class="hl-code"> = </span><span class="hl-builtin">int</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">输入一个年份: </span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">year</span><span class="hl-code"> % </span><span
                        class="hl-number">4</span><span class="hl-brackets">)</span><span
                        class="hl-code"> == </span><span class="hl-number">0</span><span class="hl-code">:
   </span><span class="hl-identifier">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">year</span><span class="hl-code"> % </span><span
                        class="hl-number">100</span><span class="hl-brackets">)</span><span
                        class="hl-code"> == </span><span class="hl-number">0</span><span class="hl-code">:
       </span><span class="hl-identifier">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">year</span><span class="hl-code"> % </span><span
                        class="hl-number">400</span><span class="hl-brackets">)</span><span
                        class="hl-code"> == </span><span class="hl-number">0</span><span class="hl-code">:
           </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">{0} 是闰年</span><span class="hl-quotes">"</span><span
                        class="hl-code">.</span><span class="hl-identifier">format</span><span
                        class="hl-brackets">(</span><span class="hl-identifier">year</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span
                        class="hl-code">   </span><span class="hl-comment"># 整百年能被400整除的是闰年</span><span class="hl-code">
       </span><span class="hl-reserved">else</span><span class="hl-code">:
           </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">{0} 不是闰年</span><span
                        class="hl-quotes">"</span><span class="hl-code">.</span><span
                        class="hl-identifier">format</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">year</span><span class="hl-brackets">)</span><span
                        class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-reserved">else</span><span class="hl-code">:
       </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">{0} 是闰年</span><span class="hl-quotes">"</span><span
                        class="hl-code">.</span><span class="hl-identifier">format</span><span
                        class="hl-brackets">(</span><span class="hl-identifier">year</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span
                        class="hl-code">       </span><span class="hl-comment"># 非整百年能被4整除的为闰年</span><span
                        class="hl-code">
</span><span class="hl-reserved">else</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">{0} 不是闰年</span><span
                        class="hl-quotes">"</span><span class="hl-code">.</span><span
                        class="hl-identifier">format</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">year</span><span class="hl-brackets">)</span><span
                        class="hl-brackets">)</span></pre>
            </div>
        </div>
        <p>我们也可以使用内嵌 if 语句来实现：</p>

        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint">输入一个年份: 2000
2000 是闰年
</pre>
        <pre class="prettyprint">输入一个年份: 2011
2011 不是闰年
</pre>

    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python 获取最大值函数</h1>


        <p>以下实例中我们使用max()方法求最大值：
        </p>
        <div class="example">
            <div class="example">实例(Python 3.0+)</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment"># -*- coding: UTF-8 -*-</span><span class="hl-code">

</span><span class="hl-comment"># Filename : test.py</span><span class="hl-code">
</span><span class="hl-comment"># author by : www.runoob.com</span><span class="hl-code">

</span><span class="hl-comment"># 最简单的</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-builtin">max</span><span class="hl-brackets">(</span><span
                        class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">2</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-builtin">max</span><span class="hl-brackets">(</span><span
                        class="hl-quotes">'</span><span class="hl-string">a</span><span class="hl-quotes">'</span><span
                        class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string">b</span><span
                        class="hl-quotes">'</span><span class="hl-brackets">)</span><span
                        class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-comment"># 也可以对列表和元组使用</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-builtin">max</span><span class="hl-brackets">(</span><span
                        class="hl-brackets">[</span><span class="hl-number">1</span><span class="hl-code">,</span><span
                        class="hl-number">2</span><span class="hl-brackets">]</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-builtin">max</span><span class="hl-brackets">(</span><span
                        class="hl-brackets">(</span><span class="hl-number">1</span><span class="hl-code">,</span><span
                        class="hl-number">2</span><span class="hl-brackets">)</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-comment"># 更多实例</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">80, 100, 1000 最大值为: </span><span class="hl-quotes">"</span><span
                        class="hl-code">, </span><span class="hl-builtin">max</span><span
                        class="hl-brackets">(</span><span class="hl-number">80</span><span
                        class="hl-code">, </span><span class="hl-number">100</span><span class="hl-code">, </span><span
                        class="hl-number">1000</span><span class="hl-brackets">)</span><span
                        class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">-20, 100, 400最大值为: </span><span class="hl-quotes">"</span><span
                        class="hl-code">, </span><span class="hl-builtin">max</span><span
                        class="hl-brackets">(</span><span class="hl-code">-</span><span class="hl-number">20</span><span
                        class="hl-code">, </span><span class="hl-number">100</span><span class="hl-code">, </span><span
                        class="hl-number">400</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">-80, -20, -10最大值为: </span><span class="hl-quotes">"</span><span
                        class="hl-code">, </span><span class="hl-builtin">max</span><span
                        class="hl-brackets">(</span><span class="hl-code">-</span><span class="hl-number">80</span><span
                        class="hl-code">, -</span><span class="hl-number">20</span><span class="hl-code">, -</span><span
                        class="hl-number">10</span><span class="hl-brackets">)</span><span
                        class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">0, 100, -400最大值为:</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span
                        class="hl-builtin">max</span><span class="hl-brackets">(</span><span
                        class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">100</span><span
                        class="hl-code">, -</span><span class="hl-number">400</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span></pre>
            </div>
        </div>

        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="lit">2</span><span class="pln">
b
</span><span class="lit">2</span><span class="pln">
</span><span class="lit">2</span><span class="pln">
</span><span class="lit">80</span><span class="pun">,</span><span class="pln"> </span><span class="lit">100</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">1000</span><span
                class="pln"> </span><span class="pun">最大值为:</span><span class="pln">  </span><span
                class="lit">1000</span><span class="pln">
</span><span class="pun">-</span><span class="lit">20</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">100</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">400</span><span class="pun">最大值为:</span><span class="pln">  </span><span
                class="lit">400</span><span class="pln">
</span><span class="pun">-</span><span class="lit">80</span><span class="pun">,</span><span class="pln"> </span><span
                class="pun">-</span><span class="lit">20</span><span class="pun">,</span><span class="pln"> </span><span
                class="pun">-</span><span class="lit">10</span><span class="pun">最大值为:</span><span class="pln">  </span><span
                class="pun">-</span><span class="lit">10</span><span class="pln">
</span><span class="lit">0</span><span class="pun">,</span><span class="pln"> </span><span class="lit">100</span><span
                class="pun">,</span><span class="pln"> </span><span class="pun">-</span><span
                class="lit">400</span><span class="pun">最大值为:</span><span class="pln"> </span><span
                class="lit">100</span></pre>

        <p>max() 函数介绍：<a href="/python/func-number-max.html">Python max()</a>函数。</p>


    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python 质数判断</h1>


        <p>
            一个大于1的自然数，除了1和它本身外，不能被其他自然数（质数）整除（2, 3, 5, 7等），换句话说就是该数除了1和它本身以外不再有其他的因数。
        </p>
        <br>
        <div class="example">
            <div class="example">test.py 文件：</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment"># -*- coding: UTF-8 -*-</span><span class="hl-code">

</span><span class="hl-comment"># Filename : test.py</span><span class="hl-code">
</span><span class="hl-comment"># author by : www.runoob.com</span><span class="hl-code">

</span><span class="hl-comment"># Python 程序用于检测用户输入的数字是否为质数</span><span class="hl-code">

</span><span class="hl-comment"># 用户输入数字</span><span class="hl-code">
</span><span class="hl-identifier">num</span><span class="hl-code"> = </span><span class="hl-builtin">int</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">请输入一个数字: </span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-comment"># 质数大于 1</span><span class="hl-code">
</span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-identifier">num</span><span
                        class="hl-code"> &gt; </span><span class="hl-number">1</span><span class="hl-code">:
   </span><span class="hl-comment"># 查看因子</span><span class="hl-code">
   </span><span class="hl-reserved">for</span><span class="hl-code"> </span><span class="hl-identifier">i</span><span
                        class="hl-code"> </span><span class="hl-reserved">in</span><span class="hl-code"> </span><span
                        class="hl-builtin">range</span><span class="hl-brackets">(</span><span
                        class="hl-number">2</span><span class="hl-code">,</span><span
                        class="hl-identifier">num</span><span class="hl-brackets">)</span><span class="hl-code">:
       </span><span class="hl-identifier">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">num</span><span class="hl-code"> % </span><span
                        class="hl-identifier">i</span><span class="hl-brackets">)</span><span
                        class="hl-code"> == </span><span class="hl-number">0</span><span class="hl-code">:
           </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-identifier">num</span><span
                        class="hl-code">,</span><span class="hl-quotes">"</span><span class="hl-string">不是质数</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
           </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-identifier">i</span><span
                        class="hl-code">,</span><span class="hl-quotes">"</span><span class="hl-string">乘于</span><span
                        class="hl-quotes">"</span><span class="hl-code">,</span><span
                        class="hl-identifier">num</span><span class="hl-code">//</span><span
                        class="hl-identifier">i</span><span class="hl-code">,</span><span
                        class="hl-quotes">"</span><span class="hl-string">是</span><span class="hl-quotes">"</span><span
                        class="hl-code">,</span><span class="hl-identifier">num</span><span class="hl-brackets">)</span><span
                        class="hl-code">
           </span><span class="hl-reserved">break</span><span class="hl-code">
   </span><span class="hl-reserved">else</span><span class="hl-code">:
       </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">num</span><span class="hl-code">,</span><span
                        class="hl-quotes">"</span><span class="hl-string">是质数</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-comment"># 如果输入的数字小于或等于 1，不是质数</span><span class="hl-code">
</span><span class="hl-reserved">else</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">num</span><span class="hl-code">,</span><span
                        class="hl-quotes">"</span><span class="hl-string">不是质数</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span></pre>
            </div>
        </div>

        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">$ python3 test</span><span
                class="pun">.</span><span class="pln">py
</span><span class="pun">请输入一个数字:</span><span class="pln"> </span><span class="lit">1</span><span class="pln">
</span><span class="lit">1</span><span class="pln"> </span><span class="pun">不是质数</span><span class="pln">
$ python3 test</span><span class="pun">.</span><span class="pln">py
</span><span class="pun">请输入一个数字:</span><span class="pln"> </span><span class="lit">4</span><span class="pln">
</span><span class="lit">4</span><span class="pln"> </span><span class="pun">不是质数</span><span class="pln">
</span><span class="lit">2</span><span class="pln"> </span><span class="pun">乘于</span><span class="pln"> </span><span
                class="lit">2</span><span class="pln"> </span><span class="pun">是</span><span class="pln"> </span><span
                class="lit">4</span><span class="pln">
$ python3 test</span><span class="pun">.</span><span class="pln">py
</span><span class="pun">请输入一个数字:</span><span class="pln"> </span><span class="lit">5</span><span class="pln">
</span><span class="lit">5</span><span class="pln"> </span><span class="pun">是质数</span></pre>



    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python 输出指定范围内的素数
        </h1>

        <p>素数（prime number）又称质数，有无限个。除了1和它本身以外不再被其他的除数整除。</p>
        <p>以下实例可以输出指定范围内的素数：</p>
        <div class="example">
            <div class="example">实例(Python 3.0+)</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">

</span><span class="hl-comment"># 输出指定范围内的素数</span><span class="hl-code">

</span><span class="hl-comment"># take input from the user</span><span class="hl-code">
</span><span class="hl-identifier">lower</span><span class="hl-code"> = </span><span class="hl-builtin">int</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">输入区间最小值: </span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">upper</span><span class="hl-code"> = </span><span class="hl-builtin">int</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">输入区间最大值: </span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-reserved">for</span><span class="hl-code"> </span><span class="hl-identifier">num</span><span
                        class="hl-code"> </span><span class="hl-reserved">in</span><span class="hl-code"> </span><span
                        class="hl-builtin">range</span><span class="hl-brackets">(</span><span class="hl-identifier">lower</span><span
                        class="hl-code">,</span><span class="hl-identifier">upper</span><span class="hl-code"> + </span><span
                        class="hl-number">1</span><span class="hl-brackets">)</span><span class="hl-code">:
    </span><span class="hl-comment"># 素数大于 1</span><span class="hl-code">
    </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-identifier">num</span><span
                        class="hl-code"> &gt; </span><span class="hl-number">1</span><span class="hl-code">:
        </span><span class="hl-reserved">for</span><span class="hl-code"> </span><span
                        class="hl-identifier">i</span><span class="hl-code"> </span><span
                        class="hl-reserved">in</span><span class="hl-code"> </span><span class="hl-builtin">range</span><span
                        class="hl-brackets">(</span><span class="hl-number">2</span><span class="hl-code">,</span><span
                        class="hl-identifier">num</span><span class="hl-brackets">)</span><span class="hl-code">:
            </span><span class="hl-identifier">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">num</span><span class="hl-code"> % </span><span
                        class="hl-identifier">i</span><span class="hl-brackets">)</span><span
                        class="hl-code"> == </span><span class="hl-number">0</span><span class="hl-code">:
                </span><span class="hl-reserved">break</span><span class="hl-code">
        </span><span class="hl-reserved">else</span><span class="hl-code">:
            </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">num</span><span class="hl-brackets">)</span></pre>
            </div>
        </div>
        <p>执行以上程序，输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">$ python3 test</span><span
                class="pun">.</span><span class="pln">py
</span><span class="pun">输入区间最小值:</span><span class="pln"> </span><span class="lit">1</span><span class="pln">
</span><span class="pun">输入区间最大值:</span><span class="pln"> </span><span class="lit">100</span><span class="pln">
</span><span class="lit">2</span><span class="pln">
</span><span class="lit">3</span><span class="pln">
</span><span class="lit">5</span><span class="pln">
</span><span class="lit">7</span><span class="pln">
</span><span class="lit">11</span><span class="pln">
</span><span class="lit">13</span><span class="pln">
</span><span class="lit">17</span><span class="pln">
</span><span class="lit">19</span><span class="pln">
</span><span class="lit">23</span><span class="pln">
</span><span class="lit">29</span><span class="pln">
</span><span class="lit">31</span><span class="pln">
</span><span class="lit">37</span><span class="pln">
</span><span class="lit">41</span><span class="pln">
</span><span class="lit">43</span><span class="pln">
</span><span class="lit">47</span><span class="pln">
</span><span class="lit">53</span><span class="pln">
</span><span class="lit">59</span><span class="pln">
</span><span class="lit">61</span><span class="pln">
</span><span class="lit">67</span><span class="pln">
</span><span class="lit">71</span><span class="pln">
</span><span class="lit">73</span><span class="pln">
</span><span class="lit">79</span><span class="pln">
</span><span class="lit">83</span><span class="pln">
</span><span class="lit">89</span><span class="pln">
</span><span class="lit">97</span></pre>



    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python 阶乘实例</h1>



        <p>
            整数的阶乘（英语：factorial）是所有小于及等于该数的正整数的积，0的阶乘为1。即：n!=1×2×3×...×n。
        </p>
        <p>
        </p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">

</span><span class="hl-comment"># Filename : test.py</span><span class="hl-code">
</span><span class="hl-comment"># author by : www.runoob.com</span><span class="hl-code">

</span><span class="hl-comment"># 通过用户输入数字计算阶乘</span><span class="hl-code">

</span><span class="hl-comment"># 获取用户输入的数字</span><span class="hl-code">
</span><span class="hl-identifier">num</span><span class="hl-code"> = </span><span class="hl-builtin">int</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">请输入一个数字: </span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">factorial</span><span class="hl-code"> = </span><span class="hl-number">1</span><span
                        class="hl-code">

</span><span class="hl-comment"># 查看数字是负数，0 或 正数</span><span class="hl-code">
</span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-identifier">num</span><span
                        class="hl-code"> &lt; </span><span class="hl-number">0</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">抱歉，负数没有阶乘</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">elif</span><span class="hl-code"> </span><span class="hl-identifier">num</span><span
                        class="hl-code"> == </span><span class="hl-number">0</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">0 的阶乘为 1</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">else</span><span class="hl-code">:
   </span><span class="hl-reserved">for</span><span class="hl-code"> </span><span class="hl-identifier">i</span><span
                        class="hl-code"> </span><span class="hl-reserved">in</span><span class="hl-code"> </span><span
                        class="hl-builtin">range</span><span class="hl-brackets">(</span><span
                        class="hl-number">1</span><span class="hl-code">,</span><span
                        class="hl-identifier">num</span><span class="hl-code"> + </span><span class="hl-number">1</span><span
                        class="hl-brackets">)</span><span class="hl-code">:
       </span><span class="hl-identifier">factorial</span><span class="hl-code"> = </span><span class="hl-identifier">factorial</span><span
                        class="hl-code">*</span><span class="hl-identifier">i</span><span class="hl-code">
   </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">%d 的阶乘为 %d</span><span
                        class="hl-quotes">"</span><span class="hl-code"> %</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">num</span><span class="hl-code">,</span><span class="hl-identifier">factorial</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span></pre>
            </div>
        </div>

        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint">请输入一个数字: 3
3 的阶乘为 6
</pre>



    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python 九九乘法表</h1>



        <p>
            以下实例演示了如何实现九九乘法表：
        </p>

        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment"># -*- coding: UTF-8 -*-</span><span class="hl-code">

</span><span class="hl-comment"># Filename : test.py</span><span class="hl-code">
</span><span class="hl-comment"># author by : www.runoob.com</span><span class="hl-code">

</span><span class="hl-comment"># 九九乘法表</span><span class="hl-code">
</span><span class="hl-reserved">for</span><span class="hl-code"> </span><span class="hl-identifier">i</span><span
                        class="hl-code"> </span><span class="hl-reserved">in</span><span class="hl-code"> </span><span
                        class="hl-builtin">range</span><span class="hl-brackets">(</span><span
                        class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">10</span><span
                        class="hl-brackets">)</span><span class="hl-code">:
        </span><span class="hl-reserved">for</span><span class="hl-code"> </span><span
                        class="hl-identifier">j</span><span class="hl-code"> </span><span
                        class="hl-reserved">in</span><span class="hl-code"> </span><span class="hl-builtin">range</span><span
                        class="hl-brackets">(</span><span class="hl-number">1</span><span class="hl-code">, </span><span
                        class="hl-identifier">i</span><span class="hl-code">+</span><span
                        class="hl-number">1</span><span class="hl-brackets">)</span><span class="hl-code">:
            </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-quotes">'</span><span class="hl-string">{}x{}={}</span><span
                        class="hl-special">\t</span><span class="hl-quotes">'</span><span class="hl-code">.</span><span
                        class="hl-identifier">format</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">i</span><span class="hl-code">, </span><span
                        class="hl-identifier">j</span><span class="hl-code">, </span><span
                        class="hl-identifier">i</span><span class="hl-code">*</span><span class="hl-identifier">j</span><span
                        class="hl-brackets">)</span><span class="hl-code">, </span><span
                        class="hl-identifier">end</span><span class="hl-code">=</span><span
                        class="hl-quotes">'</span><span class="hl-quotes">'</span><span
                        class="hl-brackets">)</span><span class="hl-code">
        </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-brackets">)</span></pre>
            </div>
        </div>

        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="lit">1x1</span><span class="pun">=</span><span
                class="lit">1</span><span class="pln">
</span><span class="lit">2x1</span><span class="pun">=</span><span class="lit">2</span><span class="pln">   </span><span
                class="lit">2x2</span><span class="pun">=</span><span class="lit">4</span><span class="pln">
</span><span class="lit">3x1</span><span class="pun">=</span><span class="lit">3</span><span class="pln">   </span><span
                class="lit">3x2</span><span class="pun">=</span><span class="lit">6</span><span
                class="pln">   </span><span class="lit">3x3</span><span class="pun">=</span><span
                class="lit">9</span><span class="pln">
</span><span class="lit">4x1</span><span class="pun">=</span><span class="lit">4</span><span class="pln">   </span><span
                class="lit">4x2</span><span class="pun">=</span><span class="lit">8</span><span
                class="pln">   </span><span class="lit">4x3</span><span class="pun">=</span><span
                class="lit">12</span><span class="pln">  </span><span class="lit">4x4</span><span
                class="pun">=</span><span class="lit">16</span><span class="pln">
</span><span class="lit">5x1</span><span class="pun">=</span><span class="lit">5</span><span class="pln">   </span><span
                class="lit">5x2</span><span class="pun">=</span><span class="lit">10</span><span
                class="pln">  </span><span class="lit">5x3</span><span class="pun">=</span><span
                class="lit">15</span><span class="pln">  </span><span class="lit">5x4</span><span
                class="pun">=</span><span class="lit">20</span><span class="pln">  </span><span
                class="lit">5x5</span><span class="pun">=</span><span class="lit">25</span><span class="pln">
</span><span class="lit">6x1</span><span class="pun">=</span><span class="lit">6</span><span class="pln">   </span><span
                class="lit">6x2</span><span class="pun">=</span><span class="lit">12</span><span
                class="pln">  </span><span class="lit">6x3</span><span class="pun">=</span><span
                class="lit">18</span><span class="pln">  </span><span class="lit">6x4</span><span
                class="pun">=</span><span class="lit">24</span><span class="pln">  </span><span
                class="lit">6x5</span><span class="pun">=</span><span class="lit">30</span><span
                class="pln">  </span><span class="lit">6x6</span><span class="pun">=</span><span
                class="lit">36</span><span class="pln">
</span><span class="lit">7x1</span><span class="pun">=</span><span class="lit">7</span><span class="pln">   </span><span
                class="lit">7x2</span><span class="pun">=</span><span class="lit">14</span><span
                class="pln">  </span><span class="lit">7x3</span><span class="pun">=</span><span
                class="lit">21</span><span class="pln">  </span><span class="lit">7x4</span><span
                class="pun">=</span><span class="lit">28</span><span class="pln">  </span><span
                class="lit">7x5</span><span class="pun">=</span><span class="lit">35</span><span
                class="pln">  </span><span class="lit">7x6</span><span class="pun">=</span><span
                class="lit">42</span><span class="pln">  </span><span class="lit">7x7</span><span
                class="pun">=</span><span class="lit">49</span><span class="pln">
</span><span class="lit">8x1</span><span class="pun">=</span><span class="lit">8</span><span class="pln">   </span><span
                class="lit">8x2</span><span class="pun">=</span><span class="lit">16</span><span
                class="pln">  </span><span class="lit">8x3</span><span class="pun">=</span><span
                class="lit">24</span><span class="pln">  </span><span class="lit">8x4</span><span
                class="pun">=</span><span class="lit">32</span><span class="pln">  </span><span
                class="lit">8x5</span><span class="pun">=</span><span class="lit">40</span><span
                class="pln">  </span><span class="lit">8x6</span><span class="pun">=</span><span
                class="lit">48</span><span class="pln">  </span><span class="lit">8x7</span><span
                class="pun">=</span><span class="lit">56</span><span class="pln">  </span><span
                class="lit">8x8</span><span class="pun">=</span><span class="lit">64</span><span class="pln">
</span><span class="lit">9x1</span><span class="pun">=</span><span class="lit">9</span><span class="pln">   </span><span
                class="lit">9x2</span><span class="pun">=</span><span class="lit">18</span><span
                class="pln">  </span><span class="lit">9x3</span><span class="pun">=</span><span
                class="lit">27</span><span class="pln">  </span><span class="lit">9x4</span><span
                class="pun">=</span><span class="lit">36</span><span class="pln">  </span><span
                class="lit">9x5</span><span class="pun">=</span><span class="lit">45</span><span
                class="pln">  </span><span class="lit">9x6</span><span class="pun">=</span><span
                class="lit">54</span><span class="pln">  </span><span class="lit">9x7</span><span
                class="pun">=</span><span class="lit">63</span><span class="pln">  </span><span
                class="lit">9x8</span><span class="pun">=</span><span class="lit">72</span><span
                class="pln">  </span><span class="lit">9x9</span><span class="pun">=</span><span
                class="lit">81</span></pre>
        <p>通过指定end参数的值，可以取消在末尾输出回车符，实现不换行。</p>


    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python 斐波那契数列</h1>



        <p>
            斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13,特别指出：第0项是0，第1项是第一个1。从第三项开始，每一项都等于前两项之和。
        </p>
        <p>Python 实现斐波那契数列代码如下：</p>
        <div class="example">
            <div class="example">实例(Python 3.0+)</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment"># -*- coding: UTF-8 -*-</span><span class="hl-code">

</span><span class="hl-comment"># Filename : test.py</span><span class="hl-code">
</span><span class="hl-comment"># author by : www.runoob.com</span><span class="hl-code">

</span><span class="hl-comment"># Python 斐波那契数列实现</span><span class="hl-code">

</span><span class="hl-comment"># 获取用户输入数据</span><span class="hl-code">
</span><span class="hl-identifier">nterms</span><span class="hl-code"> = </span><span class="hl-builtin">int</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">你需要几项？</span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-comment"># 第一和第二项</span><span class="hl-code">
</span><span class="hl-identifier">n1</span><span class="hl-code"> = </span><span class="hl-number">0</span><span
                        class="hl-code">
</span><span class="hl-identifier">n2</span><span class="hl-code"> = </span><span class="hl-number">1</span><span
                        class="hl-code">
</span><span class="hl-identifier">count</span><span class="hl-code"> = </span><span class="hl-number">2</span><span
                        class="hl-code">

</span><span class="hl-comment"># 判断输入的值是否合法</span><span class="hl-code">
</span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-identifier">nterms</span><span
                        class="hl-code"> &lt;= </span><span class="hl-number">0</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">请输入一个正整数。</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">elif</span><span class="hl-code"> </span><span class="hl-identifier">nterms</span><span
                        class="hl-code"> == </span><span class="hl-number">1</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">斐波那契数列：</span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">n1</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">else</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">斐波那契数列：</span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">n1</span><span class="hl-code">,</span><span
                        class="hl-quotes">"</span><span class="hl-string">,</span><span class="hl-quotes">"</span><span
                        class="hl-code">,</span><span class="hl-identifier">n2</span><span class="hl-code">,</span><span
                        class="hl-identifier">end</span><span class="hl-code">=</span><span
                        class="hl-quotes">"</span><span class="hl-string"> , </span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-reserved">while</span><span class="hl-code"> </span><span
                        class="hl-identifier">count</span><span class="hl-code"> &lt; </span><span
                        class="hl-identifier">nterms</span><span class="hl-code">:
       </span><span class="hl-identifier">nth</span><span class="hl-code"> = </span><span
                        class="hl-identifier">n1</span><span class="hl-code"> + </span><span
                        class="hl-identifier">n2</span><span class="hl-code">
       </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">nth</span><span class="hl-code">,</span><span
                        class="hl-identifier">end</span><span class="hl-code">=</span><span
                        class="hl-quotes">"</span><span class="hl-string"> , </span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
       </span><span class="hl-comment"># 更新值</span><span class="hl-code">
       </span><span class="hl-identifier">n1</span><span class="hl-code"> = </span><span class="hl-identifier">n2</span><span
                        class="hl-code">
       </span><span class="hl-identifier">n2</span><span class="hl-code"> = </span><span
                        class="hl-identifier">nth</span><span class="hl-code">
       </span><span class="hl-identifier">count</span><span class="hl-code"> += </span><span class="hl-number">1</span>
                </pre>
            </div>
        </div>

        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">你需要几项？</span><span class="pln"> </span><span
                class="lit">10</span><span class="pln">
</span><span class="pun">斐波那契数列：</span><span class="pln">
</span><span class="lit">0</span><span class="pln"> </span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">1</span><span class="pln"> </span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">1</span><span class="pln"> </span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">2</span><span class="pln"> </span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">3</span><span class="pln"> </span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">5</span><span class="pln"> </span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">8</span><span class="pln"> </span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">13</span><span class="pln"> </span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">21</span><span class="pln"> </span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">34</span><span class="pln"> </span><span class="pun">,</span></pre>



    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python 阿姆斯特朗数</h1>



        <p>如果一个n位正整数等于其各位数字的n次方之和,则称该数为阿姆斯特朗数。
            例如1^3 + 5^3 + 3^3 = 153。</p>
        <p>1000以内的阿姆斯特朗数：

            1, 2, 3, 4, 5, 6, 7, 8, 9,
            153, 370, 371, 407。</p>
        <p>以下代码用于检测用户输入的数字是否为阿姆斯特朗数：</p>
        <div class="example">
            <div class="example">实例(Python 3.0+)</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment"># Filename : test.py</span><span class="hl-code">
</span><span class="hl-comment"># author by : www.runoob.com</span><span class="hl-code">

</span><span class="hl-comment"># Python 检测用户输入的数字是否为阿姆斯特朗数</span><span class="hl-code">

</span><span class="hl-comment"># 获取用户输入的数字</span><span class="hl-code">
</span><span class="hl-identifier">num</span><span class="hl-code"> = </span><span class="hl-builtin">int</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">请输入一个数字: </span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-comment"># 初始化变量 sum</span><span class="hl-code">
</span><span class="hl-identifier">sum</span><span class="hl-code"> = </span><span class="hl-number">0</span><span
                        class="hl-code">
</span><span class="hl-comment"># 指数</span><span class="hl-code">
</span><span class="hl-identifier">n</span><span class="hl-code"> = </span><span class="hl-builtin">len</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">str</span><span
                        class="hl-brackets">(</span><span class="hl-identifier">num</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-comment"># 检测</span><span class="hl-code">
</span><span class="hl-identifier">temp</span><span class="hl-code"> = </span><span
                        class="hl-identifier">num</span><span class="hl-code">
</span><span class="hl-reserved">while</span><span class="hl-code"> </span><span class="hl-identifier">temp</span><span
                        class="hl-code"> &gt; </span><span class="hl-number">0</span><span class="hl-code">:
   </span><span class="hl-identifier">digit</span><span class="hl-code"> = </span><span
                        class="hl-identifier">temp</span><span class="hl-code"> % </span><span
                        class="hl-number">10</span><span class="hl-code">
   </span><span class="hl-identifier">sum</span><span class="hl-code"> += </span><span
                        class="hl-identifier">digit</span><span class="hl-code"> ** </span><span
                        class="hl-identifier">n</span><span class="hl-code">
   </span><span class="hl-identifier">temp</span><span class="hl-code"> //= </span><span
                        class="hl-number">10</span><span class="hl-code">

</span><span class="hl-comment"># 输出结果</span><span class="hl-code">
</span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-identifier">num</span><span
                        class="hl-code"> == </span><span class="hl-identifier">sum</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">num</span><span class="hl-code">,</span><span
                        class="hl-quotes">"</span><span class="hl-string">是阿姆斯特朗数</span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">else</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">num</span><span class="hl-code">,</span><span
                        class="hl-quotes">"</span><span class="hl-string">不是阿姆斯特朗数</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span></pre>
            </div>
        </div>


        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">$ python3 test</span><span
                class="pun">.</span><span class="pln">py
</span><span class="pun">请输入一个数字:</span><span class="pln"> </span><span class="lit">345</span><span class="pln">
</span><span class="lit">345</span><span class="pln"> </span><span class="pun">不是阿姆斯特朗数</span><span class="pln">

$ python3 test</span><span class="pun">.</span><span class="pln">py
</span><span class="pun">请输入一个数字:</span><span class="pln"> </span><span class="lit">153</span><span class="pln">
</span><span class="lit">153</span><span class="pln"> </span><span class="pun">是阿姆斯特朗数</span><span class="pln">

$ python3 test</span><span class="pun">.</span><span class="pln">py
</span><span class="pun">请输入一个数字:</span><span class="pln"> </span><span class="lit">1634</span><span class="pln">
</span><span class="lit">1634</span><span class="pln"> </span><span class="pun">是阿姆斯特朗数</span></pre>
        <h3>获取指定期间内的阿姆斯特朗数</h3>
        <div class="example">
            <div class="example">实例(Python 3.0+)</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment"># Filename ：test.py</span><span class="hl-code">
</span><span class="hl-comment"># author by : www.runoob.com</span><span class="hl-code">

</span><span class="hl-comment"># 获取用户输入数字</span><span class="hl-code">
</span><span class="hl-identifier">lower</span><span class="hl-code"> = </span><span class="hl-builtin">int</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">最小值: </span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span
                        class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">upper</span><span class="hl-code"> = </span><span class="hl-builtin">int</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">最大值: </span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span
                        class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-reserved">for</span><span class="hl-code"> </span><span class="hl-identifier">num</span><span
                        class="hl-code"> </span><span class="hl-reserved">in</span><span class="hl-code"> </span><span
                        class="hl-builtin">range</span><span class="hl-brackets">(</span><span class="hl-identifier">lower</span><span
                        class="hl-code">,</span><span class="hl-identifier">upper</span><span class="hl-code"> + </span><span
                        class="hl-number">1</span><span class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-comment"># 初始化 sum</span><span class="hl-code">
   </span><span class="hl-identifier">sum</span><span class="hl-code"> = </span><span class="hl-number">0</span><span
                        class="hl-code">
   </span><span class="hl-comment"># 指数</span><span class="hl-code">
   </span><span class="hl-identifier">n</span><span class="hl-code"> = </span><span class="hl-builtin">len</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">str</span><span
                        class="hl-brackets">(</span><span class="hl-identifier">num</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">

   </span><span class="hl-comment"># 检测</span><span class="hl-code">
   </span><span class="hl-identifier">temp</span><span class="hl-code"> = </span><span
                        class="hl-identifier">num</span><span class="hl-code">
   </span><span class="hl-reserved">while</span><span class="hl-code"> </span><span
                        class="hl-identifier">temp</span><span class="hl-code"> &gt; </span><span
                        class="hl-number">0</span><span class="hl-code">:
       </span><span class="hl-identifier">digit</span><span class="hl-code"> = </span><span
                        class="hl-identifier">temp</span><span class="hl-code"> % </span><span
                        class="hl-number">10</span><span class="hl-code">
       </span><span class="hl-identifier">sum</span><span class="hl-code"> += </span><span
                        class="hl-identifier">digit</span><span class="hl-code"> ** </span><span
                        class="hl-identifier">n</span><span class="hl-code">
       </span><span class="hl-identifier">temp</span><span class="hl-code"> //= </span><span class="hl-number">10</span><span
                        class="hl-code">

   </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-identifier">num</span><span
                        class="hl-code"> == </span><span class="hl-identifier">sum</span><span class="hl-code">:
       </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">num</span><span class="hl-brackets">)</span></pre>
            </div>
        </div>

        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">最小值:</span><span class="pln"> </span><span
                class="lit">1</span><span class="pln">
</span><span class="pun">最大值:</span><span class="pln"> </span><span class="lit">10000</span><span class="pln">
</span><span class="lit">1</span><span class="pln">
</span><span class="lit">2</span><span class="pln">
</span><span class="lit">3</span><span class="pln">
</span><span class="lit">4</span><span class="pln">
</span><span class="lit">5</span><span class="pln">
</span><span class="lit">6</span><span class="pln">
</span><span class="lit">7</span><span class="pln">
</span><span class="lit">8</span><span class="pln">
</span><span class="lit">9</span><span class="pln">
</span><span class="lit">153</span><span class="pln">
</span><span class="lit">370</span><span class="pln">
</span><span class="lit">371</span><span class="pln">
</span><span class="lit">407</span><span class="pln">
</span><span class="lit">1634</span><span class="pln">
</span><span class="lit">8208</span><span class="pln">
</span><span class="lit">9474</span></pre>
        <p>以上实例中我们输出了 1 到 10000 之间的阿姆斯特朗数。</p>


    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python 十进制转二进制、八进制、十六进制</h1>



        <p>以下代码用于实现十进制转二进制、八进制、十六进制：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com"># -*- coding: UTF-8 -*-</span><span
                class="pln">

</span><span class="com"># Filename : test.py</span><span class="pln">
</span><span class="com"># author by : www.runoob.com</span><span class="pln">

</span><span class="com"># 获取用户输入十进制数</span><span class="pln">
dec </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">int</span><span
                class="pun">(</span><span class="pln">input</span><span class="pun">(</span><span
                class="str">"输入数字："</span><span class="pun">))</span><span class="pln">

</span><span class="kwd">print</span><span class="pun">(</span><span class="str">"十进制数为："</span><span
                class="pun">,</span><span class="pln"> dec</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="str">"转换为二进制为："</span><span
                class="pun">,</span><span class="pln"> bin</span><span class="pun">(</span><span
                class="pln">dec</span><span class="pun">))</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="str">"转换为八进制为："</span><span
                class="pun">,</span><span class="pln"> oct</span><span class="pun">(</span><span
                class="pln">dec</span><span class="pun">))</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="str">"转换为十六进制为："</span><span
                class="pun">,</span><span class="pln"> hex</span><span class="pun">(</span><span
                class="pln">dec</span><span class="pun">))</span></pre>

        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">python3 test</span><span
                class="pun">.</span><span class="pln">py
</span><span class="pun">输入数字：</span><span class="lit">5</span><span class="pln">
</span><span class="pun">十进制数为：</span><span class="lit">5</span><span class="pln">
</span><span class="pun">转换为二进制为：</span><span class="pln"> </span><span class="lit">0b101</span><span class="pln">
</span><span class="pun">转换为八进制为：</span><span class="pln"> </span><span class="lit">0o5</span><span class="pln">
</span><span class="pun">转换为十六进制为：</span><span class="pln"> </span><span class="lit">0x5</span></pre>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">python3 test</span><span
                class="pun">.</span><span class="pln">py
</span><span class="pun">输入数字：</span><span class="lit">12</span><span class="pln">
</span><span class="pun">十进制数为：</span><span class="lit">12</span><span class="pln">
</span><span class="pun">转换为二进制为：</span><span class="pln"> </span><span class="lit">0b1100</span><span class="pln">
</span><span class="pun">转换为八进制为：</span><span class="pln"> </span><span class="lit">0o14</span><span class="pln">
</span><span class="pun">转换为十六进制为：</span><span class="pln"> </span><span class="lit">0xc</span></pre>



    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python ASCII码与字符相互转换</h1>



        <p>以下代码用于实现ASCII码与字符相互转换：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com"># Filename : test.py</span><span class="pln">
</span><span class="com"># author by : www.runoob.com</span><span class="pln">

</span><span class="com"># 用户输入字符</span><span class="pln">
c </span><span class="pun">=</span><span class="pln"> input</span><span class="pun">(</span><span class="str">"请输入一个字符: "</span><span
                class="pun">)</span><span class="pln">

</span><span class="com"># 用户输入ASCII码，并将输入的数字转为整型</span><span class="pln">
a </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">int</span><span class="pun">(</span><span
                class="pln">input</span><span class="pun">(</span><span class="str">"请输入一个ASCII码: "</span><span
                class="pun">))</span><span class="pln">


</span><span class="kwd">print</span><span class="pun">(</span><span class="pln"> c </span><span
                class="pun">+</span><span class="pln"> </span><span class="str">" 的ASCII 码为"</span><span
                class="pun">,</span><span class="pln"> ord</span><span class="pun">(</span><span
                class="pln">c</span><span class="pun">))</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln"> a </span><span
                class="pun">,</span><span class="pln"> </span><span class="str">" 对应的字符为"</span><span
                class="pun">,</span><span class="pln"> chr</span><span class="pun">(</span><span
                class="pln">a</span><span class="pun">))</span></pre>

        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">python3 test</span><span
                class="pun">.</span><span class="pln">py
</span><span class="pun">请输入一个字符:</span><span class="pln"> a
</span><span class="pun">请输入一个</span><span class="pln">ASCII</span><span class="pun">码:</span><span class="pln"> </span><span
                class="lit">101</span><span class="pln">
a </span><span class="pun">的</span><span class="pln">ASCII </span><span class="pun">码为</span><span
                class="pln"> </span><span class="lit">97</span><span class="pln">
</span><span class="lit">101</span><span class="pln">  </span><span class="pun">对应的字符为</span><span class="pln"> e</span></pre>



    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python 最大公约数算法</h1>



        <p>以下代码用于实现最大公约数算法：</p>
        <div class="example">
            <div class="example">实例(Python 3.0+)</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment"># Filename : test.py</span><span class="hl-code">
</span><span class="hl-comment"># author by : www.runoob.com</span><span class="hl-code">

</span><span class="hl-comment"># 定义一个函数</span><span class="hl-code">
</span><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-identifier">hcf</span><span
                        class="hl-brackets">(</span><span class="hl-identifier">x</span><span
                        class="hl-code">, </span><span class="hl-identifier">y</span><span
                        class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-quotes">"""</span><span class="hl-string">该函数返回两个数的最大公约数</span><span
                        class="hl-quotes">"""</span><span class="hl-code">

   </span><span class="hl-comment"># 获取最小值</span><span class="hl-code">
   </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-identifier">x</span><span
                        class="hl-code"> &gt; </span><span class="hl-identifier">y</span><span class="hl-code">:
       </span><span class="hl-identifier">smaller</span><span class="hl-code"> = </span><span
                        class="hl-identifier">y</span><span class="hl-code">
   </span><span class="hl-reserved">else</span><span class="hl-code">:
       </span><span class="hl-identifier">smaller</span><span class="hl-code"> = </span><span
                        class="hl-identifier">x</span><span class="hl-code">

   </span><span class="hl-reserved">for</span><span class="hl-code"> </span><span class="hl-identifier">i</span><span
                        class="hl-code"> </span><span class="hl-reserved">in</span><span class="hl-code"> </span><span
                        class="hl-builtin">range</span><span class="hl-brackets">(</span><span
                        class="hl-number">1</span><span class="hl-code">,</span><span
                        class="hl-identifier">smaller</span><span class="hl-code"> + </span><span
                        class="hl-number">1</span><span class="hl-brackets">)</span><span class="hl-code">:
       </span><span class="hl-identifier">if</span><span class="hl-brackets">(</span><span
                        class="hl-brackets">(</span><span class="hl-identifier">x</span><span class="hl-code"> % </span><span
                        class="hl-identifier">i</span><span class="hl-code"> == </span><span
                        class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span
                        class="hl-identifier">and</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">y</span><span class="hl-code"> % </span><span
                        class="hl-identifier">i</span><span class="hl-code"> == </span><span
                        class="hl-number">0</span><span class="hl-brackets">)</span><span
                        class="hl-brackets">)</span><span class="hl-code">:
           </span><span class="hl-identifier">hcf</span><span class="hl-code"> = </span><span
                        class="hl-identifier">i</span><span class="hl-code">

   </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span
                        class="hl-identifier">hcf</span><span class="hl-code">


</span><span class="hl-comment"># 用户输入两个数字</span><span class="hl-code">
</span><span class="hl-identifier">num1</span><span class="hl-code"> = </span><span class="hl-builtin">int</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">输入第一个数字: </span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">num2</span><span class="hl-code"> = </span><span class="hl-builtin">int</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">输入第二个数字: </span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span
                        class="hl-identifier">num1</span><span class="hl-code">,</span><span
                        class="hl-quotes">"</span><span class="hl-string">和</span><span class="hl-quotes">"</span><span
                        class="hl-code">, </span><span class="hl-identifier">num2</span><span
                        class="hl-code">,</span><span class="hl-quotes">"</span><span
                        class="hl-string">的最大公约数为</span><span class="hl-quotes">"</span><span
                        class="hl-code">, </span><span class="hl-identifier">hcf</span><span
                        class="hl-brackets">(</span><span class="hl-identifier">num1</span><span
                        class="hl-code">, </span><span class="hl-identifier">num2</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span></pre>
            </div>
        </div>

        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">输入第一个数字:</span><span class="pln"> </span><span
                class="lit">54</span><span class="pln">
</span><span class="pun">输入第二个数字:</span><span class="pln"> </span><span class="lit">24</span><span class="pln">
</span><span class="lit">54</span><span class="pln"> </span><span class="pun">和</span><span class="pln"> </span><span
                class="lit">24</span><span class="pln"> </span><span class="pun">的最大公约数为</span><span
                class="pln"> </span><span class="lit">6</span></pre>


    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python 最小公倍数算法</h1>



        <p>以下代码用于实现最小公倍数算法：</p>
        <div class="example">
            <div class="example">实例(Python 3.0+)</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment"># Filename : test.py</span><span class="hl-code">
</span><span class="hl-comment"># author by : www.runoob.com</span><span class="hl-code">

</span><span class="hl-comment"># 定义函数</span><span class="hl-code">
</span><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-identifier">lcm</span><span
                        class="hl-brackets">(</span><span class="hl-identifier">x</span><span
                        class="hl-code">, </span><span class="hl-identifier">y</span><span
                        class="hl-brackets">)</span><span class="hl-code">:

   </span><span class="hl-comment">#  获取最大的数</span><span class="hl-code">
   </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-identifier">x</span><span
                        class="hl-code"> &gt; </span><span class="hl-identifier">y</span><span class="hl-code">:
       </span><span class="hl-identifier">greater</span><span class="hl-code"> = </span><span
                        class="hl-identifier">x</span><span class="hl-code">
   </span><span class="hl-reserved">else</span><span class="hl-code">:
       </span><span class="hl-identifier">greater</span><span class="hl-code"> = </span><span
                        class="hl-identifier">y</span><span class="hl-code">

   </span><span class="hl-identifier">while</span><span class="hl-brackets">(</span><span
                        class="hl-reserved">True</span><span class="hl-brackets">)</span><span class="hl-code">:
       </span><span class="hl-identifier">if</span><span class="hl-brackets">(</span><span
                        class="hl-brackets">(</span><span class="hl-identifier">greater</span><span
                        class="hl-code"> % </span><span class="hl-identifier">x</span><span
                        class="hl-code"> == </span><span class="hl-number">0</span><span
                        class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-identifier">and</span><span
                        class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">greater</span><span class="hl-code"> % </span><span class="hl-identifier">y</span><span
                        class="hl-code"> == </span><span class="hl-number">0</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">:
           </span><span class="hl-identifier">lcm</span><span class="hl-code"> = </span><span class="hl-identifier">greater</span><span
                        class="hl-code">
           </span><span class="hl-reserved">break</span><span class="hl-code">
       </span><span class="hl-identifier">greater</span><span class="hl-code"> += </span><span
                        class="hl-number">1</span><span class="hl-code">

   </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span
                        class="hl-identifier">lcm</span><span class="hl-code">


</span><span class="hl-comment"># 获取用户输入</span><span class="hl-code">
</span><span class="hl-identifier">num1</span><span class="hl-code"> = </span><span class="hl-builtin">int</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">输入第一个数字: </span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">num2</span><span class="hl-code"> = </span><span class="hl-builtin">int</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">输入第二个数字: </span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span
                        class="hl-identifier">num1</span><span class="hl-code">,</span><span
                        class="hl-quotes">"</span><span class="hl-string">和</span><span class="hl-quotes">"</span><span
                        class="hl-code">, </span><span class="hl-identifier">num2</span><span
                        class="hl-code">,</span><span class="hl-quotes">"</span><span
                        class="hl-string">的最小公倍数为</span><span class="hl-quotes">"</span><span
                        class="hl-code">, </span><span class="hl-identifier">lcm</span><span
                        class="hl-brackets">(</span><span class="hl-identifier">num1</span><span
                        class="hl-code">, </span><span class="hl-identifier">num2</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span></pre>
            </div>
        </div>

        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">输入第一个数字:</span><span class="pln"> </span><span
                class="lit">54</span><span class="pln">
</span><span class="pun">输入第二个数字:</span><span class="pln"> </span><span class="lit">24</span><span class="pln">
</span><span class="lit">54</span><span class="pln"> </span><span class="pun">和</span><span class="pln"> </span><span
                class="lit">24</span><span class="pln"> </span><span class="pun">的最小公倍数为</span><span
                class="pln"> </span><span class="lit">216</span></pre>


    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python 简单计算器实现</h1>



        <p>以下代码用于实现简单计算器实现，包括两个数基本的加减乘除运输：</p>
        <div class="example">
            <div class="example">实例(Python 3.0+)</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment"># Filename : test.py</span><span class="hl-code">
</span><span class="hl-comment"># author by : www.runoob.com</span><span class="hl-code">

</span><span class="hl-comment"># 定义函数</span><span class="hl-code">
</span><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-identifier">add</span><span
                        class="hl-brackets">(</span><span class="hl-identifier">x</span><span
                        class="hl-code">, </span><span class="hl-identifier">y</span><span
                        class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-quotes">"""</span><span class="hl-string">相加</span><span class="hl-quotes">"""</span><span
                        class="hl-code">

   </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">x</span><span
                        class="hl-code"> + </span><span class="hl-identifier">y</span><span class="hl-code">

</span><span class="hl-reserved">def</span><span class="hl-code"> </span><span
                        class="hl-identifier">subtract</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">x</span><span class="hl-code">, </span><span
                        class="hl-identifier">y</span><span class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-quotes">"""</span><span class="hl-string">相减</span><span class="hl-quotes">"""</span><span
                        class="hl-code">

   </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">x</span><span
                        class="hl-code"> - </span><span class="hl-identifier">y</span><span class="hl-code">

</span><span class="hl-reserved">def</span><span class="hl-code"> </span><span
                        class="hl-identifier">multiply</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">x</span><span class="hl-code">, </span><span
                        class="hl-identifier">y</span><span class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-quotes">"""</span><span class="hl-string">相乘</span><span class="hl-quotes">"""</span><span
                        class="hl-code">

   </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">x</span><span
                        class="hl-code"> * </span><span class="hl-identifier">y</span><span class="hl-code">

</span><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-identifier">divide</span><span
                        class="hl-brackets">(</span><span class="hl-identifier">x</span><span
                        class="hl-code">, </span><span class="hl-identifier">y</span><span
                        class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-quotes">"""</span><span class="hl-string">相除</span><span class="hl-quotes">"""</span><span
                        class="hl-code">

   </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">x</span><span
                        class="hl-code"> / </span><span class="hl-identifier">y</span><span class="hl-code">

</span><span class="hl-comment"># 用户输入</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">选择运算：</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">1、相加</span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">2、相减</span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">3、相乘</span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">4、相除</span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">choice</span><span class="hl-code"> = </span><span
                        class="hl-builtin">input</span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">输入你的选择(1/2/3/4):</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">num1</span><span class="hl-code"> = </span><span class="hl-builtin">int</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">输入第一个数字: </span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">num2</span><span class="hl-code"> = </span><span class="hl-builtin">int</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">输入第二个数字: </span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-identifier">choice</span><span
                        class="hl-code"> == </span><span class="hl-quotes">'</span><span class="hl-string">1</span><span
                        class="hl-quotes">'</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">num1</span><span class="hl-code">,</span><span
                        class="hl-quotes">"</span><span class="hl-string">+</span><span class="hl-quotes">"</span><span
                        class="hl-code">,</span><span class="hl-identifier">num2</span><span
                        class="hl-code">,</span><span class="hl-quotes">"</span><span class="hl-string">=</span><span
                        class="hl-quotes">"</span><span class="hl-code">, </span><span
                        class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-identifier">num1</span><span
                        class="hl-code">,</span><span class="hl-identifier">num2</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-reserved">elif</span><span class="hl-code"> </span><span class="hl-identifier">choice</span><span
                        class="hl-code"> == </span><span class="hl-quotes">'</span><span class="hl-string">2</span><span
                        class="hl-quotes">'</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">num1</span><span class="hl-code">,</span><span
                        class="hl-quotes">"</span><span class="hl-string">-</span><span class="hl-quotes">"</span><span
                        class="hl-code">,</span><span class="hl-identifier">num2</span><span
                        class="hl-code">,</span><span class="hl-quotes">"</span><span class="hl-string">=</span><span
                        class="hl-quotes">"</span><span class="hl-code">, </span><span
                        class="hl-identifier">subtract</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">num1</span><span class="hl-code">,</span><span
                        class="hl-identifier">num2</span><span class="hl-brackets">)</span><span
                        class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-reserved">elif</span><span class="hl-code"> </span><span class="hl-identifier">choice</span><span
                        class="hl-code"> == </span><span class="hl-quotes">'</span><span class="hl-string">3</span><span
                        class="hl-quotes">'</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">num1</span><span class="hl-code">,</span><span
                        class="hl-quotes">"</span><span class="hl-string">*</span><span class="hl-quotes">"</span><span
                        class="hl-code">,</span><span class="hl-identifier">num2</span><span
                        class="hl-code">,</span><span class="hl-quotes">"</span><span class="hl-string">=</span><span
                        class="hl-quotes">"</span><span class="hl-code">, </span><span
                        class="hl-identifier">multiply</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">num1</span><span class="hl-code">,</span><span
                        class="hl-identifier">num2</span><span class="hl-brackets">)</span><span
                        class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-reserved">elif</span><span class="hl-code"> </span><span class="hl-identifier">choice</span><span
                        class="hl-code"> == </span><span class="hl-quotes">'</span><span class="hl-string">4</span><span
                        class="hl-quotes">'</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">num1</span><span class="hl-code">,</span><span
                        class="hl-quotes">"</span><span class="hl-string">/</span><span class="hl-quotes">"</span><span
                        class="hl-code">,</span><span class="hl-identifier">num2</span><span
                        class="hl-code">,</span><span class="hl-quotes">"</span><span class="hl-string">=</span><span
                        class="hl-quotes">"</span><span class="hl-code">, </span><span
                        class="hl-identifier">divide</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">num1</span><span class="hl-code">,</span><span
                        class="hl-identifier">num2</span><span class="hl-brackets">)</span><span
                        class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">else</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">非法输入</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span></pre>
            </div>
        </div>

        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">选择运算：</span><span class="pln">
</span><span class="lit">1</span><span class="pun">、相加</span><span class="pln">
</span><span class="lit">2</span><span class="pun">、相减</span><span class="pln">
</span><span class="lit">3</span><span class="pun">、相乘</span><span class="pln">
</span><span class="lit">4</span><span class="pun">、相除</span><span class="pln">
</span><span class="pun">输入你的选择(</span><span class="lit">1</span><span class="pun">/</span><span
                class="lit">2</span><span class="pun">/</span><span class="lit">3</span><span class="pun">/</span><span
                class="lit">4</span><span class="pun">):</span><span class="lit">2</span><span class="pln">
</span><span class="pun">输入第一个数字:</span><span class="pln"> </span><span class="lit">5</span><span class="pln">
</span><span class="pun">输入第二个数字:</span><span class="pln"> </span><span class="lit">2</span><span class="pln">
</span><span class="lit">5</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> </span><span
                class="lit">2</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span
                class="lit">3</span></pre>


    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python 生成日历</h1>



        <p>以下代码用于生成指定日期的日历：</p>
        <div class="example">
            <div class="example">实例(Python 3.0+)</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment"># Filename : test.py</span><span class="hl-code">
</span><span class="hl-comment"># author by : www.runoob.com</span><span class="hl-code">

</span><span class="hl-comment"># 引入日历模块</span><span class="hl-code">
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span
                        class="hl-identifier">calendar</span><span class="hl-code">

</span><span class="hl-comment"># 输入指定年月</span><span class="hl-code">
</span><span class="hl-identifier">yy</span><span class="hl-code"> = </span><span class="hl-builtin">int</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">输入年份: </span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">mm</span><span class="hl-code"> = </span><span class="hl-builtin">int</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">输入月份: </span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-comment"># 显示日历</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">calendar</span><span class="hl-code">.</span><span class="hl-identifier">month</span><span
                        class="hl-brackets">(</span><span class="hl-identifier">yy</span><span
                        class="hl-code">,</span><span class="hl-identifier">mm</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span></pre>
            </div>
        </div>

        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">输入年份:</span><span class="pln"> </span><span
                class="lit">2015</span><span class="pln">
</span><span class="pun">输入月份:</span><span class="pln"> </span><span class="lit">6</span><span class="pln">
     </span><span class="typ">June</span><span class="pln"> </span><span class="lit">2015</span><span class="pln">
</span><span class="typ">Mo</span><span class="pln"> </span><span class="typ">Tu</span><span class="pln"> </span><span
                class="typ">We</span><span class="pln"> </span><span class="typ">Th</span><span
                class="pln"> </span><span class="typ">Fr</span><span class="pln"> </span><span
                class="typ">Sa</span><span class="pln"> </span><span class="typ">Su</span><span class="pln">
 </span><span class="lit">1</span><span class="pln">  </span><span class="lit">2</span><span class="pln">  </span><span
                class="lit">3</span><span class="pln">  </span><span class="lit">4</span><span
                class="pln">  </span><span class="lit">5</span><span class="pln">  </span><span
                class="lit">6</span><span class="pln">  </span><span class="lit">7</span><span class="pln">
 </span><span class="lit">8</span><span class="pln">  </span><span class="lit">9</span><span class="pln"> </span><span
                class="lit">10</span><span class="pln"> </span><span class="lit">11</span><span
                class="pln"> </span><span class="lit">12</span><span class="pln"> </span><span
                class="lit">13</span><span class="pln"> </span><span class="lit">14</span><span class="pln">
</span><span class="lit">15</span><span class="pln"> </span><span class="lit">16</span><span class="pln"> </span><span
                class="lit">17</span><span class="pln"> </span><span class="lit">18</span><span
                class="pln"> </span><span class="lit">19</span><span class="pln"> </span><span
                class="lit">20</span><span class="pln"> </span><span class="lit">21</span><span class="pln">
</span><span class="lit">22</span><span class="pln"> </span><span class="lit">23</span><span class="pln"> </span><span
                class="lit">24</span><span class="pln"> </span><span class="lit">25</span><span
                class="pln"> </span><span class="lit">26</span><span class="pln"> </span><span
                class="lit">27</span><span class="pln"> </span><span class="lit">28</span><span class="pln">
</span><span class="lit">29</span><span class="pln"> </span><span class="lit">30</span></pre>


    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python 使用递归斐波那契数列</h1>



        <p>以下代码使用递归的方式来生成斐波那契数列：</p>
        <div class="example">
            <div class="example">实例(Python 3.0+)</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment"># Filename : test.py</span><span class="hl-code">
</span><span class="hl-comment"># author by : www.runoob.com</span><span class="hl-code">

</span><span class="hl-reserved">def</span><span class="hl-code"> </span><span
                        class="hl-identifier">recur_fibo</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">n</span><span class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-quotes">"""</span><span class="hl-string">递归函数
   输出斐波那契数列</span><span class="hl-quotes">"""</span><span class="hl-code">
   </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-identifier">n</span><span
                        class="hl-code"> &lt;= </span><span class="hl-number">1</span><span class="hl-code">:
       </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span
                        class="hl-identifier">n</span><span class="hl-code">
   </span><span class="hl-reserved">else</span><span class="hl-code">:
       </span><span class="hl-identifier">return</span><span class="hl-brackets">(</span><span class="hl-identifier">recur_fibo</span><span
                        class="hl-brackets">(</span><span class="hl-identifier">n</span><span
                        class="hl-code">-</span><span class="hl-number">1</span><span class="hl-brackets">)</span><span
                        class="hl-code"> + </span><span class="hl-identifier">recur_fibo</span><span
                        class="hl-brackets">(</span><span class="hl-identifier">n</span><span
                        class="hl-code">-</span><span class="hl-number">2</span><span class="hl-brackets">)</span><span
                        class="hl-brackets">)</span><span class="hl-code">


</span><span class="hl-comment"># 获取用户输入</span><span class="hl-code">
</span><span class="hl-identifier">nterms</span><span class="hl-code"> = </span><span class="hl-builtin">int</span><span
                        class="hl-brackets">(</span><span class="hl-builtin">input</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">"</span><span
                        class="hl-string">您要输出几项? </span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-comment"># 检查输入的数字是否正确</span><span class="hl-code">
</span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-identifier">nterms</span><span
                        class="hl-code"> &lt;= </span><span class="hl-number">0</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">输入正数</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">else</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">斐波那契数列:</span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-reserved">for</span><span class="hl-code"> </span><span class="hl-identifier">i</span><span
                        class="hl-code"> </span><span class="hl-reserved">in</span><span class="hl-code"> </span><span
                        class="hl-builtin">range</span><span class="hl-brackets">(</span><span class="hl-identifier">nterms</span><span
                        class="hl-brackets">)</span><span class="hl-code">:
       </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-identifier">recur_fibo</span><span
                        class="hl-brackets">(</span><span class="hl-identifier">i</span><span
                        class="hl-brackets">)</span><span class="hl-brackets">)</span></pre>
            </div>
        </div>

        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">您要输出几项?</span><span class="pln"> </span><span
                class="lit">10</span><span class="pln">
</span><span class="pun">斐波那契数列:</span><span class="pln">
</span><span class="lit">0</span><span class="pln">
</span><span class="lit">1</span><span class="pln">
</span><span class="lit">1</span><span class="pln">
</span><span class="lit">2</span><span class="pln">
</span><span class="lit">3</span><span class="pln">
</span><span class="lit">5</span><span class="pln">
</span><span class="lit">8</span><span class="pln">
</span><span class="lit">13</span><span class="pln">
</span><span class="lit">21</span><span class="pln">
</span><span class="lit">34</span></pre>


    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python 文件 IO</h1>



        <p>以下代码演示了Python基本的文件操作，包括 open，read，write：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com"># Filename : test.py</span><span class="pln">
</span><span class="com"># author by : www.runoob.com</span><span class="pln">

</span><span class="com"># 写文件</span><span class="pln">
</span><span class="kwd">with</span><span class="pln"> open</span><span class="pun">(</span><span
                class="str">"test.txt"</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">"wt"</span><span class="pun">)</span><span class="pln"> </span><span
                class="kwd">as</span><span class="pln"> out_file</span><span class="pun">:</span><span class="pln">
    out_file</span><span class="pun">.</span><span class="pln">write</span><span class="pun">(</span><span class="str">"该文本会写入到文件中\n看到我了吧！"</span><span
                class="pun">)</span><span class="pln">

</span><span class="com"># Read a file</span><span class="pln">
</span><span class="kwd">with</span><span class="pln"> open</span><span class="pun">(</span><span
                class="str">"test.txt"</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">"rt"</span><span class="pun">)</span><span class="pln"> </span><span
                class="kwd">as</span><span class="pln"> in_file</span><span class="pun">:</span><span class="pln">
    text </span><span class="pun">=</span><span class="pln"> in_file</span><span class="pun">.</span><span class="pln">read</span><span
                class="pun">()</span><span class="pln">

</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">text</span><span
                class="pun">)</span></pre>

        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">该文本会写入到文件中</span><span class="pln">
</span><span class="pun">看到我了吧！</span></pre>


    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python 字符串判断</h1>



        <p>以下代码演示了Python字符串的判断：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com"># Filename : test.py</span><span class="pln">
</span><span class="com"># author by : www.runoob.com</span><span class="pln">

</span><span class="com"># 测试实例一</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="str">"测试实例一"</span><span
                class="pun">)</span><span class="pln">
str </span><span class="pun">=</span><span class="pln"> </span><span class="str">"runoob.com"</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">str</span><span
                class="pun">.</span><span class="pln">isalnum</span><span class="pun">())</span><span
                class="pln"> </span><span class="com"># 判断所有字符都是数字或者字母</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">str</span><span
                class="pun">.</span><span class="pln">isalpha</span><span class="pun">())</span><span
                class="pln"> </span><span class="com"># 判断所有字符都是字母</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">str</span><span
                class="pun">.</span><span class="pln">isdigit</span><span class="pun">())</span><span
                class="pln"> </span><span class="com"># 判断所有字符都是数字</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">str</span><span
                class="pun">.</span><span class="pln">islower</span><span class="pun">())</span><span
                class="pln"> </span><span class="com"># 判断所有字符都是小写</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">str</span><span
                class="pun">.</span><span class="pln">isupper</span><span class="pun">())</span><span
                class="pln"> </span><span class="com"># 判断所有字符都是大写</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">str</span><span
                class="pun">.</span><span class="pln">istitle</span><span class="pun">())</span><span
                class="pln"> </span><span class="com"># 判断所有单词都是首字母大写，像标题</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">str</span><span
                class="pun">.</span><span class="pln">isspace</span><span class="pun">())</span><span
                class="pln"> </span><span class="com"># 判断所有字符都是空白字符、\t、\n、\r</span><span class="pln">

</span><span class="kwd">print</span><span class="pun">(</span><span class="str">"------------------------"</span><span
                class="pun">)</span><span class="pln">

</span><span class="com"># 测试实例二</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="str">"测试实例二"</span><span
                class="pun">)</span><span class="pln">
str </span><span class="pun">=</span><span class="pln"> </span><span class="str">"runoob"</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">str</span><span
                class="pun">.</span><span class="pln">isalnum</span><span class="pun">())</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">str</span><span
                class="pun">.</span><span class="pln">isalpha</span><span class="pun">())</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">str</span><span
                class="pun">.</span><span class="pln">isdigit</span><span class="pun">())</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">str</span><span
                class="pun">.</span><span class="pln">islower</span><span class="pun">())</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">str</span><span
                class="pun">.</span><span class="pln">isupper</span><span class="pun">())</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">str</span><span
                class="pun">.</span><span class="pln">istitle</span><span class="pun">())</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">str</span><span
                class="pun">.</span><span class="pln">isspace</span><span class="pun">())</span><span
                class="pln"> </span></pre>

        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">测试实例一</span><span class="pln">
</span><span class="kwd">False</span><span class="pln">
</span><span class="kwd">False</span><span class="pln">
</span><span class="kwd">False</span><span class="pln">
</span><span class="kwd">True</span><span class="pln">
</span><span class="kwd">False</span><span class="pln">
</span><span class="kwd">False</span><span class="pln">
</span><span class="kwd">False</span><span class="pln">
</span><span class="pun">------------------------</span><span class="pln">
</span><span class="pun">测试实例二</span><span class="pln">
</span><span class="kwd">True</span><span class="pln">
</span><span class="kwd">True</span><span class="pln">
</span><span class="kwd">False</span><span class="pln">
</span><span class="kwd">True</span><span class="pln">
</span><span class="kwd">False</span><span class="pln">
</span><span class="kwd">False</span></pre>


    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python 字符串大小写转换</h1>



        <p>以下代码演示了如何将字符串转换为大写字母，或者将字符串转为小写字母等：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com"># Filename : test.py</span><span class="pln">
</span><span class="com"># author by : www.runoob.com</span><span class="pln">

str </span><span class="pun">=</span><span class="pln"> </span><span class="str">"www.runoob.com"</span><span
                class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">str</span><span
                class="pun">.</span><span class="pln">upper</span><span class="pun">())</span><span
                class="pln">          </span><span class="com"># 把所有字符中的小写字母转换成大写字母</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">str</span><span
                class="pun">.</span><span class="pln">lower</span><span class="pun">())</span><span
                class="pln">          </span><span class="com"># 把所有字符中的大写字母转换成小写字母</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">str</span><span
                class="pun">.</span><span class="pln">capitalize</span><span class="pun">())</span><span
                class="pln">     </span><span class="com"># 把第一个字母转化为大写字母，其余小写</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">str</span><span
                class="pun">.</span><span class="pln">title</span><span class="pun">())</span><span
                class="pln">          </span><span class="com"># 把每个单词的第一个字母转化为大写，其余小写 </span></pre>

        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">WWW</span><span class="pun">.</span><span
                class="pln">RUNOOB</span><span class="pun">.</span><span class="pln">COM
www</span><span class="pun">.</span><span class="pln">runoob</span><span class="pun">.</span><span class="pln">com
</span><span class="typ">Www</span><span class="pun">.</span><span class="pln">runoob</span><span
                class="pun">.</span><span class="pln">com
</span><span class="typ">Www</span><span class="pun">.</span><span class="typ">Runoob</span><span
                class="pun">.</span><span class="typ">Com</span></pre>


    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python 计算每个月天数</h1>



        <p>以下代码通过导入 calendar 模块来计算每个月的天数：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com"># Filename : test.py</span><span class="pln">
</span><span class="com"># author by : www.runoob.com</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> calendar
monthRange </span><span class="pun">=</span><span class="pln"> calendar</span><span class="pun">.</span><span
                class="pln">monthrange</span><span class="pun">(</span><span class="lit">2016</span><span
                class="pun">,</span><span class="lit">9</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">monthRange</span><span
                class="pun">)</span></pre>

        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">(</span><span class="lit">3</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">30</span><span
                class="pun">)</span></pre>

        <p>输出的是一个元组，第一个元素是所查月份的第一天对应的是星期几（0-6），第二个元素是这个月的天数。以上实例输出的意思为 2016 年 9 月份的第一天是星期四，该月总共有 30 天。
        </p>


    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python 获取昨天日期</h1>



        <p>以下代码通过导入 datetime 模块来获取昨天的日期：</p>
        <div class="example">
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment"># Filename : test.py</span><span class="hl-code">
</span><span class="hl-comment"># author by : www.runoob.com</span><span class="hl-code">

</span><span class="hl-comment"># 引入 datetime 模块</span><span class="hl-code">
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span
                        class="hl-identifier">datetime</span><span class="hl-code">
</span><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-identifier">getYesterday</span><span
                        class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">:
    </span><span class="hl-identifier">today</span><span class="hl-code">=</span><span
                        class="hl-identifier">datetime</span><span class="hl-code">.</span><span class="hl-identifier">date</span><span
                        class="hl-code">.</span><span class="hl-identifier">today</span><span
                        class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
    </span><span class="hl-identifier">oneday</span><span class="hl-code">=</span><span
                        class="hl-identifier">datetime</span><span class="hl-code">.</span><span class="hl-identifier">timedelta</span><span
                        class="hl-brackets">(</span><span class="hl-identifier">days</span><span
                        class="hl-code">=</span><span class="hl-number">1</span><span class="hl-brackets">)</span><span
                        class="hl-code">
    </span><span class="hl-identifier">yesterday</span><span class="hl-code">=</span><span
                        class="hl-identifier">today</span><span class="hl-code">-</span><span class="hl-identifier">oneday</span><span
                        class="hl-code">
    </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span
                        class="hl-identifier">yesterday</span><span class="hl-code">

</span><span class="hl-comment"># 输出</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-identifier">getYesterday</span><span
                        class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span>
                </pre>
            </div>
        </div>

        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="lit">2015</span><span class="pun">-</span><span
                class="lit">06</span><span class="pun">-</span><span class="lit">10</span></pre>

        <p>以上实例输出的意思为昨天的日期是 2015 年 6 月 10 日。
        </p>


    </div>
</div>


<div class="article-body">

    <div class="article-intro">

        <h1>Python list 常用操作</h1>


        <h2>1.list 定义</h2>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> li </span><span class="pun">=</span><span class="pln"> </span><span
                class="pun">[</span><span class="str">"a"</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">"b"</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">"mpilgrim"</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">"z"</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">"example"</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li
</span><span class="pun">[</span><span class="str">'a'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'b'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'mpilgrim'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'z'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'example'</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li</span><span class="pun">[</span><span
                class="lit">1</span><span class="pun">]</span><span class="pln">
</span><span class="str">'b'</span></pre>

        <h2>2.list 负数索引</h2>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li
</span><span class="pun">[</span><span class="str">'a'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'b'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'mpilgrim'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'z'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'example'</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li</span><span class="pun">[-</span><span
                class="lit">1</span><span class="pun">]</span><span class="pln">
</span><span class="str">'example'</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li</span><span class="pun">[-</span><span
                class="lit">3</span><span class="pun">]</span><span class="pln">
</span><span class="str">'mpilgrim'</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li
</span><span class="pun">[</span><span class="str">'a'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'b'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'mpilgrim'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'z'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'example'</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li</span><span class="pun">[</span><span
                class="lit">1</span><span class="pun">:</span><span class="lit">3</span><span class="pun">]</span><span
                class="pln">
</span><span class="pun">[</span><span class="str">'b'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'mpilgrim'</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li</span><span class="pun">[</span><span
                class="lit">1</span><span class="pun">:-</span><span class="lit">1</span><span class="pun">]</span><span
                class="pln">
</span><span class="pun">[</span><span class="str">'b'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'mpilgrim'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'z'</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li</span><span class="pun">[</span><span
                class="lit">0</span><span class="pun">:</span><span class="lit">3</span><span class="pun">]</span><span
                class="pln">
</span><span class="pun">[</span><span class="str">'a'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'b'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'mpilgrim'</span><span class="pun">]</span><span class="pln"> </span></pre>
        <h2> 3.list 增加元素</h2>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li
</span><span class="pun">[</span><span class="str">'a'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'b'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'mpilgrim'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'z'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'example'</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li</span><span class="pun">.</span><span class="pln">append</span><span
                class="pun">(</span><span class="str">"new"</span><span class="pun">)</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li
</span><span class="pun">[</span><span class="str">'a'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'b'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'mpilgrim'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'z'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'example'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'new'</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li</span><span class="pun">.</span><span class="pln">insert</span><span
                class="pun">(</span><span class="lit">2</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">"new"</span><span class="pun">)</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li
</span><span class="pun">[</span><span class="str">'a'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'b'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'new'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'mpilgrim'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'z'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'example'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'new'</span><span
                class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li</span><span class="pun">.</span><span class="pln">extend</span><span
                class="pun">([</span><span class="str">"two"</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">"elements"</span><span class="pun">])</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li
</span><span class="pun">[</span><span class="str">'a'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'b'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'new'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'mpilgrim'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'z'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'example'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'new'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'two'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'elements'</span><span
                class="pun">]</span></pre>
        <h2>4.list 搜索 </h2>

        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li
</span><span class="pun">[</span><span class="str">'a'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'b'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'new'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'mpilgrim'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'z'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'example'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'new'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'two'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'elements'</span><span
                class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li</span><span class="pun">.</span><span class="pln">index</span><span
                class="pun">(</span><span class="str">"example"</span><span class="pun">)</span><span class="pln">
</span><span class="lit">5</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li</span><span class="pun">.</span><span class="pln">index</span><span
                class="pun">(</span><span class="str">"new"</span><span class="pun">)</span><span class="pln">
</span><span class="lit">2</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li</span><span class="pun">.</span><span class="pln">index</span><span
                class="pun">(</span><span class="str">"c"</span><span class="pun">)</span><span class="pln">
</span><span class="typ">Traceback</span><span class="pln"> </span><span class="pun">(</span><span
                class="pln">innermost </span><span class="kwd">last</span><span class="pun">):</span><span class="pln">
 </span><span class="typ">File</span><span class="pln"> </span><span class="str">"&lt;interactive input&gt;"</span><span
                class="pun">,</span><span class="pln"> line </span><span class="lit">1</span><span
                class="pun">,</span><span class="pln"> </span><span class="kwd">in</span><span class="pln"> </span><span
                class="pun">?</span><span class="pln">
</span><span class="typ">ValueError</span><span class="pun">:</span><span class="pln"> list</span><span
                class="pun">.</span><span class="pln">index</span><span class="pun">(</span><span
                class="pln">x</span><span class="pun">):</span><span class="pln"> x </span><span
                class="kwd">not</span><span class="pln"> </span><span class="kwd">in</span><span class="pln"> list
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="str">"c"</span><span
                class="pln"> </span><span class="kwd">in</span><span class="pln"> li
</span><span class="kwd">False</span></pre>
        <h2>5.list 删除元素</h2>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li
</span><span class="pun">[</span><span class="str">'a'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'b'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'new'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'mpilgrim'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'z'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'example'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'new'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'two'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'elements'</span><span
                class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li</span><span class="pun">.</span><span class="pln">remove</span><span
                class="pun">(</span><span class="str">"z"</span><span class="pun">)</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li
</span><span class="pun">[</span><span class="str">'a'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'b'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'new'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'mpilgrim'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'example'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'new'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'two'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'elements'</span><span
                class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li</span><span class="pun">.</span><span class="pln">remove</span><span
                class="pun">(</span><span class="str">"new"</span><span class="pun">)</span><span
                class="pln">    </span><span class="com"># 删除首次出现的一个值</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li
</span><span class="pun">[</span><span class="str">'a'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'b'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'mpilgrim'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'example'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'new'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'two'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'elements'</span><span
                class="pun">]</span><span class="pln">    </span><span class="com"># 第二个 'new' 未删除</span><span
                class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li</span><span class="pun">.</span><span class="pln">remove</span><span
                class="pun">(</span><span class="str">"c"</span><span class="pun">)</span><span class="pln">     </span><span
                class="com">#list 中没有找到值, Python 会引发一个异常</span><span class="pln">
</span><span class="typ">Traceback</span><span class="pln"> </span><span class="pun">(</span><span
                class="pln">innermost </span><span class="kwd">last</span><span class="pun">):</span><span class="pln">
 </span><span class="typ">File</span><span class="pln"> </span><span class="str">"&lt;interactive input&gt;"</span><span
                class="pun">,</span><span class="pln"> line </span><span class="lit">1</span><span
                class="pun">,</span><span class="pln"> </span><span class="kwd">in</span><span class="pln"> </span><span
                class="pun">?</span><span class="pln">
</span><span class="typ">ValueError</span><span class="pun">:</span><span class="pln"> list</span><span
                class="pun">.</span><span class="pln">remove</span><span class="pun">(</span><span
                class="pln">x</span><span class="pun">):</span><span class="pln"> x </span><span
                class="kwd">not</span><span class="pln"> </span><span class="kwd">in</span><span class="pln"> list
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li</span><span class="pun">.</span><span
                class="pln">pop</span><span class="pun">()</span><span class="pln">      </span><span class="com"># pop 会做两件事: 删除 list 的最后一个元素, 然后返回删除元素的值。</span><span
                class="pln">
</span><span class="str">'elements'</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li
</span><span class="pun">[</span><span class="str">'a'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'b'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'mpilgrim'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'example'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'new'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'two'</span><span
                class="pun">]</span></pre>

        <h2>6.list 运算符</h2>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> li </span><span class="pun">=</span><span class="pln"> </span><span
                class="pun">[</span><span class="str">'a'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'b'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'mpilgrim'</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li </span><span class="pun">=</span><span
                class="pln"> li </span><span class="pun">+</span><span class="pln"> </span><span
                class="pun">[</span><span class="str">'example'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'new'</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li
</span><span class="pun">[</span><span class="str">'a'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'b'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'mpilgrim'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'example'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'new'</span><span
                class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li </span><span class="pun">+=</span><span
                class="pln"> </span><span class="pun">[</span><span class="str">'two'</span><span
                class="pun">]</span><span class="pln">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li
</span><span class="pun">[</span><span class="str">'a'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'b'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'mpilgrim'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'example'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'new'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'two'</span><span
                class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li </span><span class="pun">=</span><span
                class="pln"> </span><span class="pun">[</span><span class="lit">1</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">2</span><span class="pun">]</span><span class="pln"> </span><span
                class="pun">*</span><span class="pln"> </span><span class="lit">3</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li
</span><span class="pun">[</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">2</span><span class="pun">,</span><span class="pln"> </span><span class="lit">1</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">2</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">2</span><span class="pun">]</span><span class="pln"> </span></pre>
        <h2>7.使用join链接list成为字符串</h2>

        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="kwd">params</span><span class="pln"> </span><span
                class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span
                class="str">"server"</span><span class="pun">:</span><span class="str">"mpilgrim"</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">"database"</span><span
                class="pun">:</span><span class="str">"master"</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">"uid"</span><span class="pun">:</span><span
                class="str">"sa"</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">"pwd"</span><span class="pun">:</span><span class="str">"secret"</span><span
                class="pun">}</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="pun">[</span><span class="str">"%s=%s"</span><span
                class="pln"> </span><span class="pun">%</span><span class="pln"> </span><span class="pun">(</span><span
                class="pln">k</span><span class="pun">,</span><span class="pln"> v</span><span class="pun">)</span><span
                class="pln"> </span><span class="kwd">for</span><span class="pln"> k</span><span
                class="pun">,</span><span class="pln"> v </span><span class="kwd">in</span><span
                class="pln"> </span><span class="kwd">params</span><span class="pun">.</span><span
                class="pln">items</span><span class="pun">()]</span><span class="pln">
</span><span class="pun">[</span><span class="str">'server=mpilgrim'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'uid=sa'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'database=master'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'pwd=secret'</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="str">";"</span><span
                class="pun">.</span><span class="pln">join</span><span class="pun">([</span><span
                class="str">"%s=%s"</span><span class="pln"> </span><span class="pun">%</span><span class="pln"> </span><span
                class="pun">(</span><span class="pln">k</span><span class="pun">,</span><span class="pln"> v</span><span
                class="pun">)</span><span class="pln"> </span><span class="kwd">for</span><span
                class="pln"> k</span><span class="pun">,</span><span class="pln"> v </span><span
                class="kwd">in</span><span class="pln"> </span><span class="kwd">params</span><span class="pun">.</span><span
                class="pln">items</span><span class="pun">()])</span><span class="pln">
</span><span class="str">'server=mpilgrim;uid=sa;database=master;pwd=secret'</span></pre>
        <p>join 只能用于元素是字符串的 list; 它不进行任何的类型强制转换。连接一个存在一个或多个非字符串元素的 list 将引发一个异常。

        </p>
        <h2>8.list 分割字符串</h2>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> li </span><span class="pun">=</span><span class="pln"> </span><span
                class="pun">[</span><span class="str">'server=mpilgrim'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'uid=sa'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'database=master'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'pwd=secret'</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> s </span><span class="pun">=</span><span
                class="pln"> </span><span class="str">";"</span><span class="pun">.</span><span
                class="pln">join</span><span class="pun">(</span><span class="pln">li</span><span
                class="pun">)</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> s
</span><span class="str">'server=mpilgrim;uid=sa;database=master;pwd=secret'</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> s</span><span class="pun">.</span><span
                class="pln">split</span><span class="pun">(</span><span class="str">";"</span><span class="pun">)</span><span
                class="pln">
</span><span class="pun">[</span><span class="str">'server=mpilgrim'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'uid=sa'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'database=master'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'pwd=secret'</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> s</span><span class="pun">.</span><span
                class="pln">split</span><span class="pun">(</span><span class="str">";"</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">1</span><span class="pun">)</span><span class="pln">
</span><span class="pun">[</span><span class="str">'server=mpilgrim'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'uid=sa;database=master;pwd=secret'</span><span
                class="pun">]</span></pre>
        <p>split 与 join 正好相反, 它将一个字符串分割成多元素 list。 </p>
        <p>
            注意, 分隔符 (";") 被完全去掉了, 它没有在返回的 list 中的任意元素中出现。 </p>
        <p>
            split 接受一个可选的第二个参数, 它是要分割的次数。</p>
        <h2>9.list 的映射解析</h2>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> li </span><span class="pun">=</span><span class="pln"> </span><span
                class="pun">[</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">9</span><span class="pun">,</span><span class="pln"> </span><span class="lit">8</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">4</span><span class="pun">]</span><span
                class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="pun">[</span><span
                class="pln">elem</span><span class="pun">*</span><span class="lit">2</span><span
                class="pln"> </span><span class="kwd">for</span><span class="pln"> elem </span><span
                class="kwd">in</span><span class="pln"> li</span><span class="pun">]</span><span class="pln">&nbsp;&nbsp;&nbsp;
</span><span class="pun">[</span><span class="lit">2</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">18</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">16</span><span class="pun">,</span><span class="pln"> </span><span class="lit">8</span><span
                class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li
</span><span class="pun">[</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">9</span><span class="pun">,</span><span class="pln"> </span><span class="lit">8</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">4</span><span class="pun">]</span><span
                class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li </span><span class="pun">=</span><span
                class="pln"> </span><span class="pun">[</span><span class="pln">elem</span><span
                class="pun">*</span><span class="lit">2</span><span class="pln"> </span><span
                class="kwd">for</span><span class="pln"> elem </span><span class="kwd">in</span><span
                class="pln"> li</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> li
</span><span class="pun">[</span><span class="lit">2</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">18</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">16</span><span class="pun">,</span><span class="pln"> </span><span class="lit">8</span><span
                class="pun">]</span><span class="pln"> </span></pre>
        <h2>10.dictionary中的解析</h2>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="kwd">params</span><span class="pln"> </span><span
                class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span
                class="str">"server"</span><span class="pun">:</span><span class="str">"mpilgrim"</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">"database"</span><span
                class="pun">:</span><span class="str">"master"</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">"uid"</span><span class="pun">:</span><span
                class="str">"sa"</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">"pwd"</span><span class="pun">:</span><span class="str">"secret"</span><span
                class="pun">}</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">params</span><span
                class="pun">.</span><span class="pln">keys</span><span class="pun">()</span><span class="pln">
</span><span class="pun">[</span><span class="str">'server'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'uid'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'database'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'pwd'</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">params</span><span
                class="pun">.</span><span class="pln">values</span><span class="pun">()</span><span class="pln">
</span><span class="pun">[</span><span class="str">'mpilgrim'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'sa'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'master'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'secret'</span><span
                class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">params</span><span
                class="pun">.</span><span class="pln">items</span><span class="pun">()</span><span class="pln">
</span><span class="pun">[(</span><span class="str">'server'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'mpilgrim'</span><span class="pun">),</span><span
                class="pln"> </span><span class="pun">(</span><span class="str">'uid'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'sa'</span><span
                class="pun">),</span><span class="pln"> </span><span class="pun">(</span><span
                class="str">'database'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'master'</span><span
                class="pun">),</span><span class="pln"> </span><span class="pun">(</span><span
                class="str">'pwd'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'secret'</span><span class="pun">)]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="pun">[</span><span
                class="pln">k </span><span class="kwd">for</span><span class="pln"> k</span><span
                class="pun">,</span><span class="pln"> v </span><span class="kwd">in</span><span
                class="pln"> </span><span class="kwd">params</span><span class="pun">.</span><span
                class="pln">items</span><span class="pun">()]</span><span class="pln">
</span><span class="pun">[</span><span class="str">'server'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'uid'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'database'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'pwd'</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="pun">[</span><span
                class="pln">v </span><span class="kwd">for</span><span class="pln"> k</span><span
                class="pun">,</span><span class="pln"> v </span><span class="kwd">in</span><span
                class="pln"> </span><span class="kwd">params</span><span class="pun">.</span><span
                class="pln">items</span><span class="pun">()]</span><span class="pln">
</span><span class="pun">[</span><span class="str">'mpilgrim'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'sa'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'master'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'secret'</span><span
                class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="pun">[</span><span class="str">"%s=%s"</span><span
                class="pln"> </span><span class="pun">%</span><span class="pln"> </span><span class="pun">(</span><span
                class="pln">k</span><span class="pun">,</span><span class="pln"> v</span><span class="pun">)</span><span
                class="pln"> </span><span class="kwd">for</span><span class="pln"> k</span><span
                class="pun">,</span><span class="pln"> v </span><span class="kwd">in</span><span
                class="pln"> </span><span class="kwd">params</span><span class="pun">.</span><span
                class="pln">items</span><span class="pun">()]</span><span class="pln">
</span><span class="pun">[</span><span class="str">'server=mpilgrim'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'uid=sa'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'database=master'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'pwd=secret'</span><span class="pun">]</span></pre>
        <h2>11.list 过滤</h2>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> li </span><span class="pun">=</span><span class="pln"> </span><span
                class="pun">[</span><span class="str">"a"</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">"mpilgrim"</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">"foo"</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">"b"</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">"c"</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">"b"</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">"d"</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">"d"</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="pun">[</span><span
                class="pln">elem </span><span class="kwd">for</span><span class="pln"> elem </span><span
                class="kwd">in</span><span class="pln"> li </span><span class="kwd">if</span><span
                class="pln"> len</span><span class="pun">(</span><span class="pln">elem</span><span class="pun">)</span><span
                class="pln"> </span><span class="pun">&gt;</span><span class="pln"> </span><span
                class="lit">1</span><span class="pun">]</span><span class="pln">
</span><span class="pun">[</span><span class="str">'mpilgrim'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'foo'</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="pun">[</span><span
                class="pln">elem </span><span class="kwd">for</span><span class="pln"> elem </span><span
                class="kwd">in</span><span class="pln"> li </span><span class="kwd">if</span><span
                class="pln"> elem </span><span class="pun">!=</span><span class="pln"> </span><span
                class="str">"b"</span><span class="pun">]</span><span class="pln">
</span><span class="pun">[</span><span class="str">'a'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'mpilgrim'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'foo'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'c'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'d'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'d'</span><span
                class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="pun">[</span><span
                class="pln">elem </span><span class="kwd">for</span><span class="pln"> elem </span><span
                class="kwd">in</span><span class="pln"> li </span><span class="kwd">if</span><span
                class="pln"> li</span><span class="pun">.</span><span class="pln">count</span><span class="pun">(</span><span
                class="pln">elem</span><span class="pun">)</span><span class="pln"> </span><span
                class="pun">==</span><span class="pln"> </span><span class="lit">1</span><span class="pun">]</span><span
                class="pln">
</span><span class="pun">[</span><span class="str">'a'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'mpilgrim'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'foo'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'c'</span><span
                class="pun">]</span></pre>



    </div>
</div>


</body>
</html>
